Find broken objects in SQL Server

前端 未结 11 535
孤独总比滥情好
孤独总比滥情好 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条回答
  •  再見小時候
    2020-12-02 17:53

    Note the query in this thread finds missing objects, not invalid ones. 
    SQL Server doesn't find a referencing object is invalid until you execute it.

    Enhancement to that query to handle objects in other schemas as well as types:

    SELECT
        '[' + OBJECT_SCHEMA_NAME(referencing_id) + '].[' + OBJECT_NAME(referencing_id) + ']' 
            AS [this sproc, UDF or VIEW...],
        isnull('[' + referenced_schema_name + '].', '') + '[' + referenced_entity_name + ']' 
            AS [... depends ON this missing entity name]
    FROM 
        sys.sql_expression_dependencies
    WHERE 
        is_ambiguous = 0 AND 
        (
            (
                [referenced_class_desc] = 'TYPE' and 
                TYPE_ID(
                    isnull('[' + referenced_schema_name + '].', '') + 
                    '[' + referenced_entity_name + ']'
                ) IS NULL
            ) or
            (   
                [referenced_class_desc] <> 'TYPE' and 
                OBJECT_ID(
                    isnull('[' + referenced_schema_name + '].', '') + 
                    '[' + referenced_entity_name + ']'
                ) IS NULL
            )
        )
    ORDER BY 
        '[' + OBJECT_SCHEMA_NAME(referencing_id) + '].[' + OBJECT_NAME(referencing_id) + ']',
        isnull('[' + referenced_schema_name + '].', '') + '[' + referenced_entity_name + ']'
    

提交回复
热议问题