Created
September 19, 2013 15:03
-
-
Save ChrisMissal/6624833 to your computer and use it in GitHub Desktop.
Find all columns that have foreign keys referencing another table.
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 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