查找引用另一个存储过程的所有存储过程

时间:2017-02-10 08:53:42

标签: sql sql-server sql-server-2008 database-administration

我想找到所有对DB对象列表的直接和间接引用。

我正在使用以下查询,但我发现了问题。

DECLARE @Search varchar(255)
SET @Search = 'Employee'

; WITH CTE AS 
(
    SELECT DISTINCT
        o.name AS Object_Name, o.type_desc
    FROM 
        sys.sql_modules m 
    INNER JOIN 
        sys.objects o ON m.object_id = o.object_id
    WHERE 
        m.definition LIKE '%' + @Search + '%'

    UNION ALL

    SELECT 
        o.name AS Object_Name, o.type_desc
    FROM 
        sys.sql_modules m 
    INNER JOIN 
        sys.objects o ON m.object_id = o.object_id
    INNER JOIN 
        cte AS c ON c.Object_Name = o.name
)
SELECT * 
FROM Cte 

但是我收到了一个错误:

  

Msg 530,Level 16,State 1,Line 3
  声明终止。在语句完成之前,最大递归100已经用尽。

1 个答案:

答案 0 :(得分:0)

这里的代码将起作用

DECLARE @Search varchar(255)
SET @Search = 'Employee'

; WITH CTE AS 
(
    SELECT DISTINCT o.name AS Object_Name, o.type_desc
    FROM sys.sql_modules m 
    INNER JOIN sys.objects o ON m.object_id = o.object_id
    WHERE m.definition LIKE '%' + @Search + '%'

    UNION ALL

    SELECT o.name AS Object_Name, o.type_desc
    FROM sys.sql_modules m 
    INNER JOIN sys.objects o ON m.object_id = o.object_id
    , cte AS c
    WHERE m.definition LIKE '%' + c.Object_Name + '%'
      AND o.name <> c.Object_Name
)
SELECT * 
FROM Cte 
;