select OBJECT_NAME(k.parent_object_id) as parentTable,
c1.name as parentColumn,
OBJECT_NAME(k.referenced_object_id) as referencedTable,
c2.name as referencedColumn,
k.name
from sys.foreign_keys k
join sys.foreign_key_columns f on f.parent_object_id = k.parent_object_id
join sys.columns c1 on c1.column_id = f.parent_column_id
and c1.object_id = k.parent_object_id
join sys.columns c2 on c2.column_id = f.referenced_column_id
and c2.object_id = k.referenced_object_id
where c2.name = 'ColumnName'
and OBJECT_NAME(k.referenced_object_id) = 'TableName'
No comments:
Post a Comment