Script to ID foreign keys referencing a table

  • Hi,

    Perhaps someone has already written this script.

    Based on a table name, I would like to identify details of the foreign keys (Table name, column names) that reference the table.

    Thanks.

    Regards,

    PK.

  • SELECT  (CASE WHEN OBJECTPROPERTY(CONSTID, 'CNSTISDISABLED') = 0 THEN 'ENABLED' ELSE 'DISABLED' END) AS STATUS,        

    OBJECT_NAME(CONSTID) AS CONSTRAINT_NAME,       

    OBJECT_NAME(FKEYID) AS TABLE_NAME,        

    COL_NAME(FKEYID, FKEY) AS COLUMN_NAME,       

    OBJECT_NAME(RKEYID) AS REFERENCED_TABLE_NAME,       

    COL_NAME(RKEYID, RKEY) AS REFERENCED_COLUMN_NAME

    FROM SYSFOREIGNKEYS

    WHERE OBJECT_NAME(RKEYID) = 'your table name here'

    ORDER BY TABLE_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME,  KEYNO

    GO

     

  • Thanks rsharma.  Your help is much appreciated.

    Regards,

    PK.

     

     

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply