尝试在游标内使用 Case 语句

发布于 2024-08-23 16:28:59 字数 2185 浏览 7 评论 0原文

我正在尝试进行以下工作:

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 语句:

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

我只想在存在 @FK 值时创建 alter table 语句,如果它是 '' 则跳过它。

有人能指出我正确的方向吗?

I am trying to make the following work:

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

Please notice the Case statement:

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

I simply want to create the alter table statement when there is a @FK value and to skip it if it's ''.

Can someone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[旋木] 2024-08-30 16:28:59

您必须使用 IF ELSE 语句。

此外,稍微格式化一下代码,将使它更容易阅读 X-)

WHILE 循环替换

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 

为 这样你的整个语句看起来像

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

You have to uase an IF ELSE statement.

Also, Formatting the code a little, will make it a LOT easier to read X-)

Replace the WHILE loop with

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 

So that your entire statement looks like

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文