views:

565

answers:

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,

+1  A: 

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
Jonathan
FK constraints might not reference the PK on the other table: any unique constraint may be used.
van
"//" is not a comment, "--" is...
KM
+1 : My answer was not complete, this is, so I deleted mine,
Peter
Doesn't work for me, but thanks anyway
Binary Worrier
+1  A: 

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
+1 for the suggestion to look at the source of the system proc, that would never have occurred to me. Thanks mate.
Binary Worrier
+2  A: 
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
Excellent, exactly what I was looking for, thanks pal
Binary Worrier
+1  A: 

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 = 

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.

Chris
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
@Ogre: I was a few months late. :)
Chris
@Chris: Well, for what it's worth, it helped me, and hopefully will help future answer-seekers.
Ogre Psalm33

related questions