Created
November 10, 2017 22:11
-
-
Save jeffjohnson9046/ff436fe579220c444dd01877f369efb9 to your computer and use it in GitHub Desktop.
MS SQL Server: for a given table, find all "child" tables that reference it
This file contains 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
-- Based on answer from here: | |
-- https://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given-table-in-sql-server | |
SELECT | |
child.name AS child_table, | |
fk.constraint_column_id AS fk_part_no, | |
c.name AS foreign_key_column | |
FROM | |
sys.foreign_key_columns AS fk | |
JOIN | |
sys.tables child | |
ON fk.parent_object_id = child.object_id | |
JOIN | |
sys.columns c | |
ON fk.parent_object_id = c.object_id | |
AND fk.parent_column_id = c.column_id | |
JOIN | |
sys.tables parent | |
ON parent.object_id = fk.referenced_object_id | |
WHERE | |
parent.name = 'tblSetOfAdminSubjects' | |
ORDER BY | |
child.name, | |
fk.constraint_column_id; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment