Stored procedure dependencies in SQL Server Management Studio

前端 未结 5 2080
抹茶落季
抹茶落季 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:12

    If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).

    enter image description here

    enter image description here

    It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??

    0 讨论(0)
  • 2021-01-14 09:18

    Use this query:

    SELECT ROUTINE_NAME, ROUTINE_DEFINITION 
    FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_DEFINITION LIKE '%YOUR COLUMN %' 
    AND ROUTINE_TYPE='PROCEDURE'
    
    0 讨论(0)
  • 2021-01-14 09:22

    List of All Dependent Objects in single query.

    select distinct A.name from sys.procedures A inner join sys.sql_dependencies B on A.object_id = B.object_id;

    OR

    select distinct A.name from sys.objects A inner join sys.sql_dependencies B on A.object_id = B.object_id where A.type_desc = 'mentioned your Object Type';

    0 讨论(0)
  • 2021-01-14 09:24

    I wonder why you cannot see the dependencies via the 'View Dependencies' dialog because it works perfectly fine for me. Nevertheless you can query the 'sys.sql_expression_dependencies' system view and obtain the dependency information that you want.

    Example

    SELECT OBJECT_NAME(referencing_id),OBJECT_NAME(referenced_id)  
    FROM sys.sql_expression_dependencies 
    WHERE referenced_id = OBJECT_ID('XXX')
    

    You can of course project other information that you might need.

    0 讨论(0)
  • 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')
    
    0 讨论(0)
提交回复
热议问题