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.
As you can see, we have extracted the Schema list of all databases.
Good luck to everyone in business and life.