SQL Server查找一个外键被引用的所有表

直接上代码:

 1 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 :外键键名 
posted @ 2017-11-19 13:57  Leon_Liu  阅读(2996)  评论(0编辑  收藏  举报