我有一个数据库,其中包含表,过程,视图和触发器的列表。 但我想要一个查询来获取一个表的所有依赖项,包括引用父表的子表。
答案 0 :(得分:49)
以下是检查依赖性的方法
方法1:使用sp_depends
sp_depends 'dbo.First'
GO
方法2:使用information_schema.routines
SELECT *
FROM information_schema.routines ISR
WHERE CHARINDEX('dbo.First', ISR.ROUTINE_DEFINITION) > 0
GO
方法3:使用DMV sys.dm_sql_referencing_entities
SELECT referencing_schema_name, referencing_entity_name,
referencing_id, referencing_class_desc, is_caller_dependent
FROM sys.dm_sql_referencing_entities ('dbo.First', 'OBJECT');
GO
答案 1 :(得分:9)
除了其他答案中描述的方法(sp_depends系统存储过程,SQL Server动态管理功能),您还可以查看SQL Server对象之间的依赖关系 - 来自SSMS。
您可以使用SSMS中的View Dependencies option。在对象资源管理器窗格中,右键单击对象,然后从上下文菜单中选择查看依赖关系选项
我自己更喜欢名为ApexSQL Search的第三方依赖查看器。它是一个免费的插件,集成到SSMS和Visual Studio中,用于SQL对象和数据文本搜索,扩展属性管理,安全对象重命名和关系可视化。
答案 2 :(得分:7)
在SQL Server 2008中,引入了两个新的动态管理功能来跟踪对象依赖性:sys.dm_sql_referenced_entities和sys.dm_sql_referencing_entities:
1 /返回引用给定实体的实体:
SELECT
referencing_schema_name, referencing_entity_name,
referencing_class_desc, is_caller_dependent
FROM sys.dm_sql_referencing_entities ('<TableName>', 'OBJECT')
2 /返回对象引用的实体:
SELECT
referenced_schema_name, referenced_entity_name, referenced_minor_name,
referenced_class_desc, is_caller_dependent, is_ambiguous
FROM sys.dm_sql_referenced_entities ('<StoredProcedureName>', 'OBJECT');
或者,您可以使用 sp_depends :
EXEC sp_depends '<TableName>'
另一种选择是使用Red Gate中名为SQL Dependency Tracker的非常有用的工具。
答案 3 :(得分:5)
查询sysdepends表:
SELECT distinct schema_name(dependentObject.uid) as schema,
dependentObject.*
FROM sysdepends d
INNER JOIN sysobjects o on d.id = o.id
INNER JOIN sysobjects dependentObject on d.depid = dependentObject.id
WHERE o.name = 'TableName'
仅查看以名称引用对象(或任何给定文本)的视图/函数/触发器/过程的方法是:
SELECT distinct schema_name(so.uid) + '.' + so.name
FROM syscomments sc
INNER JOIN sysobjects so on sc.id = so.id
WHERE sc.text like '%Name%'
答案 4 :(得分:5)
查找所有外键
SELECT src.name, srcCol.name, dst.name, dstCol.name
FROM sys.foreign_key_columns fk
INNER JOIN sys.columns srcCol ON fk.parent_column_id = srcCol.[column_id]
AND fk.parent_object_id = srcCol.[object_id]
INNER JOIN sys.tables src ON src.[object_id] = fk.parent_object_id
INNER JOIN sys.tables dst ON dst.[object_id] = fk.[referenced_object_id]
INNER JOIN sys.columns dstCol ON fk.referenced_column_id = dstCol.[column_id]
AND fk.[referenced_object_id] = dstCol.[object_id]
答案 5 :(得分:2)
方法1:使用sp_depends
sp_depends 'dbo.First'
GO
方法2:对存储过程使用sys.procedures
select Name from sys.procedures where OBJECT_DEFINITION(OBJECT_ID) like '%Any Keyword Name%'
'%Any Keyword Name%'是您要查找的搜索关键字
方法3:使用视图的sys.views
select Name from sys.views where OBJECT_DEFINITION(OBJECT_ID) like '%Any Keyword Name%'
'%Any Keyword Name%'是您要查找的搜索关键字
答案 6 :(得分:1)
您可以使用名为Advanced SQL Server Dependencies的免费工具 http://advancedsqlserverdependencies.codeplex.com/
它支持所有数据库对象(表,视图等),并且可以在多个数据库中找到依赖关系(如果是同义词)。
答案 7 :(得分:1)
在SQL Server 2008或更高版本中,我使用以下查询来查找给定表的所有相关存储过程,用户定义函数,触发器等:
SELECT
coalesce(Referenced_server_name+'.','')+ --possible server name if cross-server
coalesce(referenced_database_name+'.','')+ --possible database name if cross-database
coalesce(referenced_schema_name+'.','')+ --likely schema name
coalesce(referenced_entity_name,'') + --very likely entity name
coalesce('.'+col_name(referenced_ID,referenced_minor_id),'')AS [referencing],
coalesce(object_schema_name(Referencing_ID)+'.','')+ --likely schema name
object_name(Referencing_ID)+ --definite entity name
coalesce('.'+col_name(referencing_ID,referencing_minor_id),'') AS [referenced]
FROM sys.sql_expression_dependencies
WHERE referenced_id =object_id('Table_name')
ORDER BY [referenced]
答案 8 :(得分:1)
答案 9 :(得分:0)
SELECT referencing_schema_name, referencing_entity_name,
case when is_caller_dependent=0 then 'NO' ELSE 'Yes'
END AS is_caller_dependent FROM sys.dm_sql_referencing_entities ('Tablename', 'OBJECT');
答案 10 :(得分:0)
我编写了下面的代码,其中列出了将找到给定关键字的所有对象。从形式上讲,它不是真正的“依赖于”搜索,而是帮助定位存储过程,视图,触发器和函数中关键字的使用位置。如果您使用的是动态SQL,则非常有用。
select name, type_desc,create_date,modify_date
from sys.all_objects o
inner join sys.all_sql_modules m on m.object_id = o.object_id
where definition like '%tableName %'
答案 11 :(得分:0)
这个问题很老但是我想在这里添加.. https://www.simple-talk.com/sql/t-sql-programming/dependencies-and-references-in-sql-server/讨论不同选项的优点和缺点,并提供存储过程(It_Depends),它产生类似于SSMS的依赖性结果树
答案 12 :(得分:0)
以下SQL列出了所有数据库和服务器上的所有对象依赖项:
IF(OBJECT_ID('tempdb..#Obj_Dep_Details') IS NOT NULL)
BEGIN
DROP TABLE #Obj_Dep_Details
END
CREATE TABLE #Obj_Dep_Details
(
[Database] nvarchar(128)
,[Schema] nvarchar(128)
,dependent_object nvarchar(128)
,dependent_object_type nvarchar(60)
,referenced_server_name nvarchar(128)
,referenced_database_name nvarchar(128)
,referenced_schema_name nvarchar(128)
,referenced_entity_name nvarchar(128)
,referenced_id int
,referenced_object_db nvarchar(128)
,referenced_object_type nvarchar(60)
,referencing_id int
,SchemaDep nvarchar(128)
)
EXEC sp_MSForEachDB @command1='USE [?];
INSERT INTO #Obj_Dep_Details
SELECT DISTINCT
DB_NAME() AS [Database]
,SCHEMA_NAME(od.[schema_id]) AS [Schema]
,OBJECT_NAME(d1.referencing_id) AS dependent_object
,od.[type_desc] AS dependent_object_type
,COALESCE(d1.referenced_server_name, @@SERVERNAME) AS referenced_server_name
,COALESCE(d1.referenced_database_name, DB_NAME()) AS referenced_database_name
,COALESCE(d1.referenced_schema_name, SCHEMA_NAME(ro.[schema_id])) AS referenced_schema_name
,d1.referenced_entity_name
,d1.referenced_id
,DB_NAME(ro.parent_object_id) AS referenced_object_db
,ro.[type_desc] AS referenced_object_type
,d1.referencing_id
,SCHEMA_NAME(od.[schema_id]) AS SchemaDep
FROM sys.sql_expression_dependencies d1
LEFT OUTER JOIN sys.all_objects od
ON d1.referencing_id = od.[object_id]
LEFT OUTER JOIN sys.objects ro
ON d1.referenced_id = ro.[object_id]'
SELECT [Database] AS [Dep_Object_DB]
,[Schema] AS [Dep_Object_Schema]
,dependent_object AS [Dep_Object_Name]
,LOWER(REPLACE(dependent_object_type, '_', ' ')) AS [Dep_Object_Type]
,referenced_server_name AS [Ref_Object_Server_Name]
,referenced_database_name AS [Ref_Object_DB]
,referenced_schema_name AS [Ref_Object_Schema]
,referenced_entity_name AS [Ref_Object_Name]
,referenced_id AS [Ref_Object_ID]
,LOWER(REPLACE(referenced_object_type, '_', ' ')) AS [Ref_Object_Type]
,referencing_id AS [Dep_Object_ID]
FROM #Obj_Dep_Details WITH(NOLOCK)
WHERE referenced_entity_name = 'TableName'
ORDER BY [Dep_Object_DB]
,[Dep_Object_Name]
,[Ref_Object_Name]
,[Ref_Object_DB]