Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 631583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:58:53+00:00 2026-05-13T19:58:53+00:00

I am trying to make the following work: declare @ActTable as varchar(1000) declare @cPK

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:58:54+00:00Added an answer on May 13, 2026 at 7:58 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do the following in jquery but can't make it work .
I'm trying to make the following bit of code to work... #include <list> template
I'm trying to make the following query work: SELECT DATE_FORMAT( date, '%Y %m' )
I'm trying to make the following work on my Debian computers and one OS
I'm trying to make the following code work with no success protected BackgroundTask<?> backgroundTask
I am trying to make the following recursive function work but keep receiving an
Trying to make a MySQL-based application support MS SQL, I ran into the following
I'm trying to make the following work but can't figure out how to combine
I'm a total beginner with OpenCL and I'm trying to make the following kernel
I trying to make a schema for the following xml. <root> <allow_any_name id=string> <required_tag_1>string</required_tag_1>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.