2016-10-11 75 views
0

我正在尋找一種方法來查看存儲過程對特定表的影響。有沒有比使用LIKE更好的方法?查詢以列出在存儲過程中使用哪些表

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

來源:Query to list all stored procedures

+0

嘗試'sp_depends將tablename' – techspider

+0

嗨喬恩,驗證http://stackoverflow.com/questions/的鏈接以供參考12622920/list-all-stored-procedures-with-schema-name –

+0

1. SP_Depends沒有提供所需的信息,並且對SP_Depends進行快速搜索,但普遍認爲它已過時並且不準確。 –

回答

0

該查詢會給你所有的存儲過程取決於表'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子句更改爲更具體或更具體,或者如果您願意,也可以添加函數,視圖等。

+0

謝謝。很好,很簡潔。 –

0

我想在SQL Server 2012中添加這些MGMT的對象,但這些說法都是我用它來尋找依賴性:

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'); 
+0

也適用。謝謝! –

0

你可以加入表中的元數據存儲過程元數據這樣:

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 
+0

由於性能影響,我正在尋找不使用「LIKE」的答案 –