Find broken objects in SQL Server

前端 未结 11 545
孤独总比滥情好
孤独总比滥情好 2020-12-02 17:26

Is there a tool that will find all objects in SQL Server (functions, procs, views) that cannot possibly work because they refer to objects that don\'t exist?

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 17:59

    You may be interested in checking out the following articles:

    • Michael J. Swart: Find Missing SQL Dependencies
    • eggheadcafe.com: Find broken stuff

    You can test Michael J. Swart's solution as follows:

    CREATE PROCEDURE proc_bad AS
        SELECT col FROM nonexisting_table
    GO
    
    SELECT
        OBJECT_NAME(referencing_id) AS [this sproc or VIEW...],
        referenced_entity_name AS [... depends ON this missing entity name]
    FROM 
        sys.sql_expression_dependencies
    WHERE 
        is_ambiguous = 0
        AND OBJECT_ID(referenced_entity_name) IS NULL
    ORDER BY 
        OBJECT_NAME(referencing_id), referenced_entity_name;
    

    Which returns:

    +------------------------+------------------------------------------+
    | this sproc or VIEW...  |  ... depends ON this missing entity name |
    |------------------------+------------------------------------------|
    | proc_bad               |  nonexisting_table                       |
    +------------------------+------------------------------------------+
    

提交回复
热议问题