2010-03-02 66 views
1

我試圖讓下面的工作中:試圖用一個case語句光標

declare @ActTable as varchar(1000) 
declare @cPK as VarChar(100) 
declare @SQL as nvarchar(2000) 
declare @FK as VarChar(100) 
declare @FKRef as VarChar(200) 
declare @TblRef as varchar (100) 


create table #temp (
M2MTable varchar(50), 
PK varchar (100), 
FK Varchar(100), 
FKRefTable Varchar(50)) 
insert into #temp 
select 'slcdpm' , 'fcustno', '','' union all 
select 'somast' , 'fsono', 'fcustno','slcdpm' union all 
select 'soitem' , 'fsono,finumber', 'fsono','somast' union all 
select 'sorels', 'fsono,finumber,frelease', 'fsono,finumber','soitem' union all 
select 'qtmast', 'fquoteno', 'fcustno', 'slcdpm' union all 
select 'qtitem' , 'fquoteno', 'fquoteno','qtmast' union all 
select 'armast', 'fcinvoice','fcustno','scldpm' union all 
select 'aritem','fcinvoice,fitem','fcinvoice','armast' union all 
select 'apvend', 'fvendno','','' union all 
select 'apmast','fvendno,fcinvoice','fvendno','apvend'union all 
--select 'apitem','fvendno,fcinvoice,union all 
select 'pomast','fpono','fvendno','apvend'union all 
select 'poitem', 'fpono,fitemno','fpono','pomast' union all 
select 'shmast', 'fshipno','fsokey','sorels'    union all 
select 'shitem','fshipno,fitemno','fshipno','shmast'  --   union all 


declare M2M_AddFK cursor for select M2MTable,FK,FKRefTable from #temp 
open M2M_AddFK 
fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
while @@FETCH_STATUS = 0 
Begin 

case 
when @FK <> ''then Set @SQL = N'alter table dbo.'+ @ActTable + ' ADD FOREIGN KEY (' + @FK + ') References DBO.' + @TblRef + '(' + @FK + ')' 
Print @SQL 
fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
else 


fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
end 

end 

close M2M_AddFK 
deallocate M2M_AddFK 

drop table #temp 

請通知的情況下聲明:

case 
when @FK <> ''then Set @SQL = N'alter table dbo.'+ @ActTable + ' ADD FOREIGN KEY (' + @FK + ') References DBO.' + @TblRef + '(' + @FK + ')' 
Print @SQL 
fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
else 


fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
end 

我只是想創建ALTER TABLE語句當有@FK值時,如果是''則跳過它。

有人能指出我正確的方向嗎?

回答

5

您必須簡化如果否則聲明。

此外,格式化代碼一點,將使其更容易閱讀X-)

while @@FETCH_STATUS = 0 
Begin 

    IF @FK <> '' 
    BEGIN 
     Set @SQL = N'alter table dbo.'+ @ActTable + ' ADD FOREIGN KEY (' + @FK + ') References DBO.' + @TblRef + '(' + @FK + ')' 
     Print @SQL 
     fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
    END 
    ELSE 
    BEGIN 
     fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
    end 
end 

使您的整個語句看起來像更換WHILE

declare @ActTable as varchar(1000) 
declare @cPK as VarChar(100) 
declare @SQL as nvarchar(2000) 
declare @FK as VarChar(100) 
declare @FKRef as VarChar(200) 
declare @TblRef as varchar (100) 


create table #temp ( 
M2MTable varchar(50), 
PK varchar (100), 
FK Varchar(100), 
FKRefTable Varchar(50)) 

insert into #temp 
select 'slcdpm' , 'fcustno', '','' union all 
select 'somast' , 'fsono', 'fcustno','slcdpm' union all 
select 'soitem' , 'fsono,finumber', 'fsono','somast' union all 
select 'sorels', 'fsono,finumber,frelease', 'fsono,finumber','soitem' union all 
select 'qtmast', 'fquoteno', 'fcustno', 'slcdpm' union all 
select 'qtitem' , 'fquoteno', 'fquoteno','qtmast' union all 
select 'armast', 'fcinvoice','fcustno','scldpm' union all 
select 'aritem','fcinvoice,fitem','fcinvoice','armast' union all 
select 'apvend', 'fvendno','','' union all 
select 'apmast','fvendno,fcinvoice','fvendno','apvend'union all 
--select 'apitem','fvendno,fcinvoice,union all 
select 'pomast','fpono','fvendno','apvend'union all 
select 'poitem', 'fpono,fitemno','fpono','pomast' union all 
select 'shmast', 'fshipno','fsokey','sorels'    union all 
select 'shitem','fshipno,fitemno','fshipno','shmast'  --   union all 


declare M2M_AddFK cursor for select M2MTable,FK,FKRefTable from #temp 

open M2M_AddFK 
fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
while @@FETCH_STATUS = 0 
Begin 

    IF @FK <> '' 
    BEGIN 
     Set @SQL = N'alter table dbo.'+ @ActTable + ' ADD FOREIGN KEY (' + @FK + ') References DBO.' + @TblRef + '(' + @FK + ')' 
     Print @SQL 
     fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
    END 
    ELSE 
    BEGIN 
     fetch next from M2M_AddFK into @ActTable,@FK,@TblRef 
    end 
end 

close M2M_AddFK 
deallocate M2M_AddFK 

drop table #temp 
+2

我想刪除ELSE,並且只有一次獲取IF後 –