SQL Server: Show all the FOREIGN KEYS that reference a TABLE

For SQL 2005+

[sourcecode language="sql"]
DECLARE
     @tableName nvarchar(MAX)
SET
    @tableName = 'tableName' -- (1)
SELECT DISTINCT
     ccu.table_name,
     ccu.constraint_name
FROM
     INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN
     INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
          ON rc.unique_constraint_name = tc.constraint_name
INNER JOIN
     INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
          ON ccu.constraint_name = rc.constraint_name
WHERE
     tc.table_name = @tableName
[/sourcecode]

Notes:

  • Change line (1) to specify the table name.
Be Sociable, Share!
This entry was posted in SQL Server. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *