Extracting Schema List of All Databases in SQL Server

Hello to everyone,

In this article, I will provide information about extracting the Schema list of all databases in SQL Server.

In SQL Server, in some cases, you may want to extract the Schema list of all databases.

You can easily do this using the code below.

DECLARE @SQL NVARCHAR(MAX);

SELECT @SQL
    = STUFF(
(
    SELECT ' UNION ALL
SELECT ' + +QUOTENAME(name, '''')
           + ' as DbName, cast(Name as varchar(128)) COLLATE DATABASE_DEFAULT 
AS Schema_Name FROM ' + QUOTENAME(name) + '.sys.schemas'
    FROM sys.databases
    ORDER BY [name]
    FOR XML PATH(''), TYPE
).value('.', 'nvarchar(max)'),
1   ,
12  ,
''
           );

SET @SQL = @SQL + N' ORDER BY DbName, Schema_Name';

EXECUTE (@SQL);

When you run the above code, you will see a result similar to the one below.

Extracting Schema List of All Databases in SQL Server

As you can see, we have extracted the Schema list of all databases.

Good luck to everyone in business and life.

332 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!