Stored procedure dependencies in SQL Server Management Studio

前端 未结 5 2084
抹茶落季
抹茶落季 2021-01-14 08:35

I don\'t know much about the MS world, but now it happens to be that I have to use SQL Server Management Studio 2008.

My problem: I have a column in a table, and I n

5条回答
  •  不知归路
    2021-01-14 09:29

    I have spent a good amount of time trying to find a way to identify column level dependencies in a quick way without having to search text or use third party applications. The other challenge is finding dependencies across multiple databases where table names may repeat, which will cause false positives when searching SP text.

    As of SQL 2008, there is a function that returns dependencies across databases on a field level.

    The code below works with a few exceptions:

    • It will fail if there are stored procedures with invalid references on tables/fields that have been deleted (Incidently I found this to be useful to find SPs that had been accidentally broken by table modifications).
    • It doesn't find all dependencies in cases where the SP is using temp tables in unusual ways.
    • In some cases I found that it was returning false positives for complex stored procedures.

    MSDN Documentation

    This code should be run from within the database where the SP is in order to be able to cross to other database dependencies.

    SELECT      
    --SP, View, or Function
    ReferencingName = o.name,
    ReferencingType = o.type_desc,
    
    --Referenced Field
    ref.referenced_database_name, --will be null if the DB is not explicitly called out
    ref.referenced_schema_name, --will be null or blank if the DB is not explicitly called out
    ref.referenced_entity_name,
    ref.referenced_minor_name
    
    FROM sys.objects AS o 
    cross apply sys.dm_sql_referenced_entities('dbo.' + o.name, 'Object') ref
    where o.type in ('FN','IF','V','P','TF')
    

提交回复
热议问题