我想找到所有依赖于给定表的数据库对象,包括通过外键引用给定表的其他表。我尝试使用“sp_depends”,它提供了存储过程、视图和触发器,但没有告诉我哪些其他表有外键引用给定表。有什么帮助吗?
我想找到所有依赖于给定表的数据库对象,包括通过外键引用给定表的其他表。我尝试使用“sp_depends”,它提供了存储过程、视图和触发器,但没有告诉我哪些其他表有外键引用给定表。有什么帮助吗?
select OBJECT_NAME(parent_object_id), OBJECT_NAME(referenced_object_id)
from sys.foreign_keys
where referenced_object_id = object_id('SchemaName.TableName')
也可以尝试这个:
USE AUX; <- use your database name
GO
SELECT f.name AS ForeignKey,
SCHEMA_NAME(f.SCHEMA_ID) SchemaName,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
SCHEMA_NAME(o.SCHEMA_ID) ReferenceSchemaName,
OBJECT_NAME(f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
GO
在最后一个GO之前,您可以添加where OBJECT_NAME(f.parent_object_id) = 'your_table_name'
以仅查看该表的依赖关系。
它将打印TableName | Column name (FK) | Reference TableName | Reference Column Name