Adsense

Monday, February 11, 2013

Query to identify stored procedure table dependencies

If you're looking to identify what stored procedures have a dependency on a table the query below will help you to find them.  If your stored procedures span multiple databases this query will still pick up dependencies, however you will need to run it in each individual database.


SELECT d.referencing_id,
    QUOTENAME(s.name),
    QUOTENAME(o.name),
    QUOTENAME(d.referenced_database_name),
    QUOTENAME(d.referenced_schema_name),
    QUOTENAME(d.referenced_entity_name),
    d.referenced_id,
    o.type_desc
FROM sys.sql_expression_dependencies AS d
INNER JOIN sys.objects AS o ON d.referencing_id = o.[object_id]
INNER JOIN sys.schemas AS s ON o.[schema_id] = s.[schema_id]