Skip to content

Instantly share code, notes, and snippets.

@ChrisMissal
Created September 19, 2013 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisMissal/6624833 to your computer and use it in GitHub Desktop.
Save ChrisMissal/6624833 to your computer and use it in GitHub Desktop.
Find all columns that have foreign keys referencing another table.
select t.name as TableWithForeignKey, fk.constraint_column_id as FK_PartNo , c.name as ForeignKeyColumn
from sys.foreign_key_columns as fk
inner join sys.tables as t on fk.parent_object_id = t.object_id
inner join sys.columns as c on fk.parent_object_id = c.object_id and fk.parent_column_id = c.column_id
where fk.referenced_object_id = (select object_id from sys.tables where name = 'tablename')
order by TableWithForeignKey, FK_PartNo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment