查找存储过程列表的所有依赖项

时间:2016-10-19 09:22:54

标签: sql-server tsql sql-server-2012

我们有一个在我们的应用程序中使用的51个存储过程的列表。

我需要找出那些存储过程调用的所有函数和存储过程的名称

有没有快速的方法可以找出我们的存储过程正在调用的内容?

我尝试使用 sys.dm_sql_referencing_entities ,如下所示

SELECT 
referencing_schema_name +'.'+ referencing_entity_name AS ReferencedEntityName,
referencing_class_desc AS ReferencingEntityDescription 
FROM sys.dm_sql_referencing_entities ('dbo.sp_GetPayRunDetails', 'OBJECT');
GO

但是我希望有一种方法可以检查所有50个存储过程,并获得有关他们调用的其他对象(存储过程和函数)的详细信息......

我不想手动执行此操作是因为我还需要对存储的proc和函数进行递归依赖...

2 个答案:

答案 0 :(得分:4)

此查询可能会为您解决问题:

WITH cteDependencies AS (
    SELECT e.referencing_id object_id, e.referencing_id, e.referenced_id, e.referenced_schema_name, e.referenced_entity_name
    FROM sys.sql_expression_dependencies e
    WHERE e.referencing_id = OBJECT_ID('dbo.sp_GetPayRunDetails')
    UNION ALL
    SELECT d.object_id, e.referencing_id, e.referenced_id, e.referenced_schema_name, e.referenced_entity_name
    FROM sys.sql_expression_dependencies e
    JOIN cteDependencies d ON d.referenced_id = e.referencing_id AND e.referenced_id <> e.referencing_id
)
SELECT OBJECT_NAME(d.object_id) source_name, d.*
    FROM cteDependencies d
    JOIN sys.all_objects o ON d.referenced_id = o.object_id
    WHERE o.[type] IN ('P','FN','TF'); -- for a list of types see https://msdn.microsoft.com/en-us/library/ms178618.aspx?f=255&MSPPError=-2147217396

请注意,循环引用会失败;如果你有这样的话,你需要跟踪依赖路径(例如在XML列中)并跳过重复的项目。

您可以通过更改OBJECT_ID('dbo.sp_GetPayRunDetails')过滤器来更改“来源”,以包含您要分析的SP。

答案 1 :(得分:2)

我有一个功能。根据您的方式修改它。它管理自我引用

CREATE FUNCTION GetDependents(    
 @ObjectName AS SYSNAME    
)    
RETURNS @result TABLE ( Seq INT IDENTITY, ObjectName SYSNAME, Hierarchy VARCHAR(128) , objNameStr varchar(max) )    
AS    
BEGIN    
;WITH Obj AS (    

SELECT DISTINCT s.id  AS ParentID, s.DepID AS ObjectID, o1.Name AS ParentName, o2.Name AS ChildName,    
       QUOTENAME(sch1.name) + '.' + QUOTENAME(o1.Name) + '(' + RTRIM(o1.type) + ')' COLLATE SQL_Latin1_General_CP1_CI_AS    
       AS ParentObject,     
     QUOTENAME(sch2.name) + '.' + QUOTENAME(o2.Name) + '(' + RTRIM(o2.type) + ')' COLLATE SQL_Latin1_General_CP1_CI_AS AS ObjectName,    
     o2.Name as objNameStr    
FROM sys.sysdepends s    
INNER JOIN sys.all_objects o1 ON s.id = o1.object_id    
INNER JOIN sys.schemas sch1 ON sch1.schema_id = o1.schema_id    
INNER JOIN sys.all_objects o2 on s.DepID = o2.object_id    
INNER JOIN sys.schemas sch2 ON sch2.schema_id = o2.schema_id    
where o2.type not in ('u') and  s.id <>s.DepID --Self Reference  ko hata...   
), cte AS (    
SELECT    
0 AS lvl,    
ParentID,    
ObjectId,    
ParentObject,    
ObjectName,    
CAST(ObjectID AS VARBINARY(512)) AS Sort    
, objNameStr    

FROM obj WHERE ParentName = @ObjectName    
UNION ALL    
SELECT    
p.lvl+ 1,    
c.ParentID,    
c.ObjectId,    
c.ParentObject,    
c.ObjectName,    
CAST(p.sort + CAST(c.ObjectID AS VARBINARY(16))    
AS VARBINARY(512)), c.objNameStr    

FROM cte p    
INNER JOIN obj c ON p.ObjectID = c.ParentID    
)    
INSERT INTO @result (ObjectName, Hierarchy,objNameStr)    
SELECT    
ObjectName,    
'|-' + REPLICATE('-',(lvl * 4)) + ObjectName,    
objNameStr    
FROM cte    
ORDER BY Sort    
OPTION (MAXRECURSION 32767);    

RETURN    
END