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

  • SEARCH
  • Home
  • 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 7553303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:00:47+00:00 2026-05-30T11:00:47+00:00

I can get the code of a stored procedure using the syscomments table. select

  • 0

I can get the code of a stored procedure using the syscomments table.

select so.name, sc.text as storedproccode
from sysobjects so (nolock)
inner join syscomments sc (nolock) on sc.id = so.id
where so.name = 'tablename'

How can I get the code for a specific foreign key constraint? I have the foreign key name.

Let me clarify what I’m looking for.

I find my table in the Object Explorer and expand the list of keys. If there is a primary key I can then right click on the primary key constraint and bring up the script in a new window.

The code looks like this:

USE [Gemini]
GO
/****** Object:  Index [PK_CM_FULFILLMENT_HISTORY]    Script Date: 02/17/2012 16:50:23 ******/
ALTER TABLE [dbo].[CM_FULFILLMENT_HISTORY] ADD  CONSTRAINT [PK_CM_FULFILLMENT_HISTORY] PRIMARY KEY NONCLUSTERED 
(
    [pan] ASC,
    [RequestCode] ASC,
    [RequestDate] ASC
)WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [GEMINI_12_Indx_FG]

I can also do this with foreign key constraints.

Is there any way to retrive the code (script) of these constraints?

Thanks

  • 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-30T11:00:48+00:00Added an answer on May 30, 2026 at 11:00 am

    I found a script that does the trick.

    Declare        @dbname varchar(128)
                ,  @tablename varchar(128)
                ,  @query nvarchar(4000)
    
    
    select   @dbname = 'gemini'
           , @tablename = 'cm_fulfillment_house'
    
    
    SET @QUERY = 
    'USE ' + @DBNAME + '
    
    
    SELECT CAST(F.NAME AS VARCHAR(255)) AS ForeignKeyName,
    CAST(c.name as varchar(255)) AS ForeignTable,
    CAST(fc.name as varchar(255)) AS ForeignColumn,
    cast(fc2.name as varchar(255)) as ForeignColumn2,
    CAST(p.name as varchar(255)) AS PrimaryTable,
    CAST(rc.name as varchar(255)) AS PrimaryColumn,
    cast(rc2.name as varchar(255)) as PrimaryColumn2
    INTO #GetFKConstraints 
    FROM SYSOBJECTS F 
    INNER JOIN SYSOBJECTS C ON F.PARENT_OBJ = C.ID
    INNER JOIN SYSREFERENCES R on F.ID = R.CONSTID 
    INNER JOIN SYSOBJECTS P ON R.RKEYID = P.ID
    INNER JOIN SYSCOLUMNS RC ON R.RKEYID = RC.ID AND R.RKEY1 = RC.COLID
    INNER JOIN SYSCOLUMNS FC ON R.FKEYID = FC.ID AND R.FKEY1 = FC.COLID
    left join syscolumns rc2 on r.rkeyid = rc2.id and r.rkey2 = rc.colid
    left join syscolumns fc2 on r.fkeyid = fc2.id and r.fkey2 = fc.colid
    WHERE F.TYPE = '+'''F'''+''
    
    select @query = @query + ' and c.name = ''' + @tablename + ''''
    
    + '
    
    --SELECT * from #GetFKConstraints'+'
    
    SELECT ' + '''ALTER TABLE ''' + '+ ForeignTable + ' + ''' 
    DROP CONSTRAINT ''' + '+ ForeignKeyName 
    FROM #GetFKConstraints 
    ORDER BY ForeignTable'
    +'
    
    SELECT ' + '''ALTER TABLE ''' + '+ ForeignTable +' + ''' 
    ADD CONSTRAINT ''' + '+ ForeignKeyName +' 
    + ''' FOREIGN KEY ('''+ ' + ForeignColumn + '+ ''') 
    REFERENCES ''' + '+ PrimaryTable + ' + '''(''' + '+ PrimaryColumn + '+ ''')''' +
    'FROM #GetFKConstraints 
    ORDER BY ForeignTable
    '
    select @query
    
    EXEC (@QUERY)
    GO 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get some percentage data from a stored procedure using code
How can I view the code of a stored procedure using sqlplus for Oracle
hello I am using a reader to get a XML from a stored procedure..
How can I get source code off the internet using SVN? I'm trying to
I was wondering how I can get the graphics card model/brand from code particularly
I need to use stored procedure to get data from DB, but I would
I'm using the Oracle command dbms_output.get_line to retrieve output from a stored procedure. Once
any idea how i can get the code below to produce this output? 1
Where can I get a code snippets for VC++ that can be used with
I can't get the code below to compile (see errors). Advice on correction would

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.