Monday, 25 August 2014

Metadata Management - Crawler Query - To get schema info from a list of servers

Please find further details on usage in my earlier posts and below is the code snippet to get schema information:
SET NOCOUNT ON
DECLARE @AllTables TABLE (
ServerName sysname,
DATABASE_NAME sysname,
TABLE_SCHEMA sysname )

DECLARE @SQLTemplate nvarchar(1500)
SET @SQLTemplate=
N' SELECT @@SERVERNAME AS Servername'
+ N', ''?'' AS DATABASE_NAME'
+ N', B.Name AS TABLE_SCHEMA FROM [?].sys.schemas B WHERE B.schema_id=1 OR B.schema_id BETWEEN 5 AND 16383;'        
  INSERT INTO @AllTables (ServerName ,
DATABASE_NAME,
TABLE_SCHEMA)

EXEC sp_msforeachdb @SQLTemplate

SET NOCOUNT OFF

SELECT * FROM @AllTables
WHERE DATABASE_NAME NOT IN ('master','model','msdb','tempdb')
ORDER BY DATABASE_NAME, TABLE_SCHEMA