Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MS SQL Server: for a given table, find all "child" tables that reference it
-- 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
You can’t perform that action at this time.