How to query list of all objects and their name from all db

C, Manoharan (Cognizant) 40 Reputation points
2025-03-20T06:51:50.58+00:00

Schema name, DB Name, Object Type, Object name etc..

SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
172 questions
0 comments No comments
{count} votes

Accepted answer
  1. LiHongMSFT-4306 31,471 Reputation points
    2025-03-20T06:59:05.7833333+00:00

    Hi @C, Manoharan (Cognizant)

    Try this:

    EXEC sp_MSforeachdb '
    USE [?];
    SELECT
        DB_NAME() AS DatabaseName,
        s.name AS SchemaName,
        o.type_desc AS ObjectType,
        o.name AS ObjectName
    FROM
        sys.objects o
        INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
    ORDER BY
        SchemaName,
        ObjectType,
        ObjectName;
    ';
    

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 46,546 Reputation points
    2025-03-20T06:56:27.8533333+00:00

    from all db

    There is no build-in function to get the informations from all databases, you have to query then from each database, e.g. using a cursor or a UNION ALL query, like

    SELECT * FROM Database01.sys.objects 
    UNION ALL 
    SELECT * FROM Database02.sys.objects
    UNION ALL ...
    
    
    0 comments No comments

  2. C, Manoharan (Cognizant) 40 Reputation points
    2025-03-20T06:59:51.8133333+00:00

    SELECT

    s.name AS SchemaName,

    o.name AS ObjectName,

    o.type_desc AS ObjectType,

    ps.used_page_count * 8 / 1024 AS SizeMB

    FROM sys.objects o

    JOIN sys.schemas s ON o.schema_id = s.schema_id

    LEFT JOIN sys.dm_db_partition_stats ps ON o.object_id = ps.object_id

    WHERE o.is_ms_shipped = 0 -- Excludes system objects

    GROUP BY s.name, o.name, o.type_desc, ps.used_page_count

    ORDER BY SchemaName, ObjectType, SizeMB DESC;

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.