【问题标题】:Query to list which tables are used in stored procedures查询以列出存储过程中使用了哪些表
【发布时间】:2016-10-11 17:47:41
【问题描述】:

我正在寻找一种方法来查看所有存储过程对特定表的影响。有没有比使用LIKE 更好的方法?

select * from DatabaseName.information_schema.routines 
where routine_type = 'PROCEDURE'
and ROUTINE_DEFINITION like '%TableName%'

来源:Query to list all stored procedures

【问题讨论】:

  • 试试sp_depends tablename
  • 嗨乔恩,验证链接以供参考stackoverflow.com/questions/12622920/…
  • 1. SP_Depends 没有提供所需的信息,对 SP_Depends 进行快速搜索,普遍的共识是它已过时且不准确。
  • 2.是的,这是正确的链接。我能够从上面的查询中获得所需的信息,但我不喜欢以这种方式使用“LIKE”进行查询。这不是很好的形式。

标签: sql sql-server stored-procedures


【解决方案1】:

此查询将为您提供依赖于表 'MyTable' 的所有存储过程:

SELECT s.name as [ObjectName], t.name as [DependsOn]  
  FROM sys.sql_expression_dependencies d
       INNER JOIN sys.objects s ON d.referencing_id = s.object_id
       INNER JOIN sys.objects t ON d.referenced_id = t.object_id
 WHERE t.name = 'MyTable' AND s.type = 'P'

您可以将 WHERE 子句更改为更具体或更具体,或者如果您愿意,还可以添加函数、视图等。

【讨论】:

  • 谢谢。简洁明了。
【解决方案2】:

我认为这些 mgmt 对象是在 SQL Server 2012 中添加的,但这些语句是我用来查找依赖项的:

SELECT
OBJECT_SCHEMA_NAME(referencing_id) AS srcschema,
OBJECT_NAME(referencing_id) AS srcname,
referencing_minor_id AS srcminorid,
referenced_schema_name AS tgtschema,
referenced_entity_name AS tgtname,
referenced_minor_id AS tgtminorid
FROM sys.sql_expression_dependencies;

SELECT
referenced_schema_name AS objschema,
referenced_entity_name AS objname,
referenced_minor_name AS minorname,
referenced_class_desc AS class
FROM sys.dm_sql_referenced_entities('dbo.ProcName', 'OBJECT');

SELECT
referencing_schema_name AS objschema,
referencing_entity_name AS objname,
referencing_class_desc AS class
FROM sys.dm_sql_referencing_entities('dbo.TableName', 'OBJECT');

【讨论】:

    【解决方案3】:

    您可以像这样将表元数据连接到存储过程元数据:

    SELECT sp.SPECIFIC_NAME
        ,t.TABLE_NAME
    FROM information_schema.routines sp
    INNER JOIN INFORMATION_SCHEMA.TABLES t 
        ON sp.ROUTINE_DEFINITION LIKE '%' + t.TABLE_NAME + '%'
    WHERE sp.routine_type = 'PROCEDURE'
    ORDER BY t.TABLE_NAME
    

    【讨论】:

    • 由于性能影响,我正在寻找不使用“LIKE”的答案
    猜你喜欢
    • 2010-09-18
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    相关资源
    最近更新 更多