Find an object in SQL Server (cross-database)

后端 未结 10 1178
太阳男子
太阳男子 2021-02-01 16:52

If I\'ve been told a table (or proc) name, but not which connected database the object is located in, is there any simple script to search for it? Maybe search somewhere in the

10条回答
  •  臣服心动
    2021-02-01 17:53

    You can achieve this by using the following query:

    EXEC sp_msforeachdb 
        'IF EXISTS
        (
            SELECT  1 
            FROM    [?].sys.objects 
            WHERE   name LIKE ''OBJECT_TO_SEARCH''
        )
        SELECT 
            ''?''       AS DB, 
            name        AS Name, 
            type_desc   AS Type 
        FROM [?].sys.objects 
        WHERE name LIKE ''OBJECT_TO_SEARCH'''
    

    Just replace OBJECT_TO_SEARCH with the actual object name you are interested in (or part of it, surrounded with %).

    More details here: https://peevsvilen.blog/2019/07/30/search-for-an-object-in-sql-server/

提交回复
热议问题