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 257355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:05:24+00:00 2026-05-11T22:05:24+00:00

Similar but NOT IDENTICAL to SQL Server 2000 – Query a Table’s Foreign Key

  • 0

Similar but NOT IDENTICAL to SQL Server 2000 – Query a Table’s Foreign Key relationships

I need a T-SQL statement that will work SQL 2000 that given a table name, will return the foreign key relationships for that table e.g.

Table MyFristTable has a foreign key to MySecondTable, where MyFirstTable.ColA must be in MySecondTable.ColB. I’d be delighted, if the sql statement (or stored proc) is ran for MyFirstTable and returned a result set on the lines of

Column | FK_Table      | FK_COLUMN
----------------------------------
ColA   | MySecondTable | ColB

NB: I have samples for SQL 2005 that won’t work because they rely on sys.foreign_key_columns

I’d rather not have to parse out the results of the sp_help statement.

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-11T22:05:24+00:00Added an answer on May 11, 2026 at 10:05 pm
    DECLARE @tableName sysname
    
    SET @tableName = '' -- Your table name goes here
    
    SELECT
        c.name
        , target.name
        , targetc.name
    FROM
        -- source table
        sysobjects t
        -- source column
        INNER JOIN syscolumns c ON t.id = c.id
        -- general constraint
        INNER JOIN sysconstraints co ON t.id = co.id AND co.colid = c.colid
        -- foreign key constraint
        INNER JOIN sysforeignkeys fk ON co.constid = fk.constid
        -- target table
        INNER JOIN sysobjects target ON fk.rkeyid = target.id
        -- target column
        INNER JOIN syscolumns targetc ON fk.rkey = targetc.colid AND fk.rkeyid = targetc.id
    WHERE
        t.name = @tableName
    

    NOTE I have I think used only those system views available in SQL 2000 (ie the sysXXX ones rather than the SQL 2005 sys.XXX ones) but I have only actually tested this in a SQL 2005 environemnt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few tables that have similar fields but not exactly the same.
There are similar question, but not regarding C# libraries I can use in my
There are a couple of questions similar to this on stack overflow but not
I asked a similar question about this previously, but I did not specify that
I have two documents both are similar but I need to find an elegant
Similar question as this one but for a Microsoft Environment. Email --> Exchange Server
I am trying to understand the following example, that is similar (but not equal)
I have already posted something similar here but I would like to ask the
I tried searching here for a similar solution but didn't see one so I
I've got a problem similar to,but subtly different from, that described here (Loading assemblies

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.