SQL Server查找一个外键被引用的所有表
时间:2021-07-01 10:21:17
帮助过:16人阅读
SELECT oSub.name
AS [子表名称] ,
2 fk.name
AS [外键名称] ,
3 SubCol.name
AS [子表列名] ,
4 oMain.name
AS [主表名称] ,
5 MainCol.name
AS [主表列名]
6 FROM sys.foreign_keys fk
7 JOIN sys.all_objects oSub
ON ( fk.parent_object_id
= oSub.
object_id )
8 JOIN sys.all_objects oMain
ON ( fk.referenced_object_id
= oMain.
object_id )
9 JOIN sys.foreign_key_columns fkCols
ON ( fk.
object_id = fkCols.constraint_object_id )
10 JOIN sys.columns SubCol
ON ( oSub.
object_id = SubCol.
object_id
11 AND fkCols.parent_column_id
= SubCol.column_id
12 )
13 JOIN sys.columns MainCol
ON ( oMain.
object_id = MainCol.
object_id
14 AND fkCols.referenced_column_id
= MainCol.column_id
15 )
16 WHERE fk.name
LIKE @FOREIGN KEY;
@FOREIGN KEY :外键键名
SQL Server查找一个外键被引用的所有表
标签:column int where for main color cts sql ref