4

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,

Community
  • 1
  • 1
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184

4 Answers4

13

I had to do this exact thing for a query, and I found this stored procedure, after trying a version much like the sys table one:

exec sp_fkeys @fktable_name = 'foo'

It looks like this is available in SQL Server 2000. Also, I found that in a few cases there were minor differences between this stored proc and the queries here. I'm guessing sp_fkeys is the canonical version.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Chris
  • 5,876
  • 3
  • 43
  • 69
  • 1
    Wow, I really I can't understand why this wasn't up-voted more! Brilliant! I would so rather remember to exec one fairly well-named stored proc than to bother with typing in a huge query to get this. – Ogre Psalm33 Mar 10 '10 at 21:58
  • @Ogre: I was a few months late. :) – Chris Mar 13 '10 at 03:25
  • Well, for what it's worth, it helped me, and hopefully will help future answer-seekers. – Ogre Psalm33 Mar 15 '10 at 20:15
6
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.

AakashM
  • 62,551
  • 17
  • 151
  • 186
1

I found this in google... so if this work is not my merit. Hope it help

 SELECT 
        FK_Table  = FK.TABLE_NAME, 
        FK_Column = CU.COLUMN_NAME, 
        PK_Table  = PK.TABLE_NAME, 
        PK_Column = PT.COLUMN_NAME, 
        Constraint_Name = C.CONSTRAINT_NAME 
    FROM 
        INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C 
        INNER JOIN 
        INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK 
            ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME 
        INNER JOIN 
        INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK 
            ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME 
        INNER JOIN 
        INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU 
            ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
        INNER JOIN 
        ( 
            SELECT 
                i1.TABLE_NAME, i2.COLUMN_NAME 
            FROM 
                INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1 
                INNER JOIN 
                INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 
                ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME 
                WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY' 
        ) PT 
        ON PT.TABLE_NAME = PK.TABLE_NAME 

    WHERE PK.TABLE_NAME='something'    -- the table for you are asking
Peter
  • 47,963
  • 46
  • 132
  • 181
Jonathan
  • 11,809
  • 5
  • 57
  • 91
1

I needed something like this once, so I just looked at the source code of the system stored procedure and copied what I needed into my own procedure and made it work as I needed.

You might look at sp_helpconstraint's source code...

KM.
  • 101,727
  • 34
  • 178
  • 212