How to list all available databases paired to their tables and attributes in Microsoft SQL Server

SQL
Microsoft-SQL
SQL-Server
Author

Galen Seilis

Published

October 24, 2023

While this isn’t a solution I intend on going with for my use cases, I thought I would note that a bird’s-eye-view of all databases paired to their respective tables in turn paired to their respective attributes can be obtained with the following relatively short script:

EXEC sp_MSforeachdb '
    USE [?];
    SELECT
        DB_NAME() AS DatabaseName,
        t.name AS TableName,
        c.name AS AttributeName
    FROM sys.tables AS t
    JOIN sys.columns AS c ON t.object_id = c.object_id;
';

Note that if you have a truly huge number of databases, tables, and respective attributes then this query will not be of much help.