Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Created November 10, 2017 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeffjohnson9046/ff436fe579220c444dd01877f369efb9 to your computer and use it in GitHub Desktop.
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
-- 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