SQL Server查找一個外鍵被引用的所有表
阿新 • • 發佈:2017-11-19
name constrain ref pre sys spa span like reference
直接上代碼:
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 JOINsys.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查找一個外鍵被引用的所有表