Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffjohnson9046/a025002aaf67686aba24 to your computer and use it in GitHub Desktop.
Save jeffjohnson9046/a025002aaf67686aba24 to your computer and use it in GitHub Desktop.
Find all tables that reference a table via foreign key relationship in MSSQL.
SELECT
OBJECT_NAME(f.parent_object_id) TableName,
COL_NAME(fc.parent_object_id,fc.parent_column_id) ColName
FROM
sys.foreign_keys AS f
INNER JOIN
sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN
sys.tables t
ON t.OBJECT_ID = fc.referenced_object_id
WHERE OBJECT_NAME (f.referenced_object_id) = 'table_name'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment