Find broken objects in SQL Server

前端 未结 11 532
孤独总比滥情好
孤独总比滥情好 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:48

    As of SQL Server 2008, a much simpler method is here:

     SELECT OBJECT_NAME(referencing_id) AS 'object making reference' ,
           referenced_class_desc ,
           referenced_schema_name ,
           referenced_entity_name AS 'object name referenced' ,
           (   SELECT object_id
               FROM   sys.objects
               WHERE  name = [referenced_entity_name]
           ) AS 'Object Found?'
    FROM   sys.sql_expression_dependencies e
           LEFT JOIN sys.tables t ON e.referenced_entity_name = t.name;
    

    As mentioned in the source article (Microsoft MSDN Article on Finding Missing Dependencies), "A 'NULL' value in the 'Object Found?' column indicates the object was not found in sys.objects."

    Example output:

    ╔═══════════════════════════════════════════════╦═══════════════════════╦════════════════════════╦═══════════════════════════════════════╦═══════════════╗
    ║            object making reference            ║ referenced_class_desc ║ referenced_schema_name ║        object name referenced         ║ Object Found? ║
    ╠═══════════════════════════════════════════════╬═══════════════════════╬════════════════════════╬═══════════════════════════════════════╬═══════════════╣
    ║ usvConversationsWithoutServerNotices          ║ OBJECT_OR_COLUMN      ║ dbo                    ║ ConversationLinesWithID               ║ NULL          ║
    ║ usvFormattedConversationLines_WithSpeakerName ║ OBJECT_OR_COLUMN      ║ dbo                    ║ ConversationLinesWithID               ║ NULL          ║
    ║ usvFormattedConversationLines_WithSpeakerName ║ OBJECT_OR_COLUMN      ║ dbo                    ║ FormattedConversationLines_Cached     ║ NULL          ║
    ║ udpCheckForDuplicates                         ║ OBJECT_OR_COLUMN      ║ dbo                    ║ FormattedConversationLines_WithChatID ║ NULL          ║
    ║ usvFormattedConversationsCombined             ║ OBJECT_OR_COLUMN      ║ dbo                    ║ GROUP_CONCAT_D                        ║ 178099675     ║
    ║ usvSequenceCrossValidationSetStudents         ║ OBJECT_OR_COLUMN      ║ dbo                    ║ usvSequenceCrossValidationSet         ║ 1406628054    ║
    ╚═══════════════════════════════════════════════╩═══════════════════════╩════════════════════════╩═══════════════════════════════════════╩═══════════════╝
    

    If you get an error that says the subquery returned more than one value, then you have more than one object with the name equaling the [referenced_entity_name], and you will need the subquery to be more specific by adding another where clause.

    You can get more information by checking sys.objects, like this:

    SELECT *
    FROM   sys.objects
    WHERE  name = [referenced_entity_name]
    

    If that information alone isn't helpful enough to figure out how to distinguish your multiple results, you may need to join sys.objects to one of the other metadata views (which are mostly documented here: https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/object-catalog-views-transact-sql?view=sql-server-ver15 ) or to sys.schemas (documented here: https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/schemas-catalog-views-sys-schemas?view=sql-server-ver15 ) to get more information.

提交回复
热议问题