我有一个包含大约 1000 个表的数据库,我需要找到与特定表相关的所有表,比如说客户表。我该如何进行?
问问题
9917 次
1 回答
11
当您要求查找与特定表相关的所有表时,我假设您要查找所有具有对“客户”表的外键引用的表。这与之前的 Stack Overflow 问题密切相关。以下使用系统目录视图的查询应该可以解决问题:
select t.name as TableWithForeignKey, fk.constraint_column_id as FK_columns , 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 = 'customers')
order by TableWithForeignKey, ForeignKeyColumn
于 2012-12-27T16:00:36.410 回答