Created
June 3, 2015 19:48
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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