List all columns in server


/ Published in: SQL
Save to your folder(s)



Copy this code and paste it in your HTML
  1. --Create temp tables to store sys data
  2. CREATE TABLE #DBList(id INT IDENTITY (1,1), DBName nvarchar(50))
  3. CREATE TABLE #TableList(id INT IDENTITY (1,1), DBName nvarchar(50), TableName Nvarchar(200), columnName nvarchar(200))
  4. CREATE TABLE #ColumnList(id INT IDENTITY (1,1), DBName nvarchar(50), TableName Nvarchar(200), tbi INT,columnName nvarchar(200))
  5.  
  6. --get a list of non system DBs
  7. INSERT INTO #DBList(DBName)
  8. SELECT name FROM master..sysDatabases WHERE sid !=0x01
  9.  
  10. --declare variables
  11. DECLARE @i INT
  12. DECLARE @max1 INT
  13. DECLARE @db nvarchar(50)
  14.  
  15. DECLARE @SQL nvarchar(4000)
  16. DECLARE @sql1 nvarchar(4000)
  17. DECLARE @tbi INT
  18. DECLARE @colID INT
  19. DECLARE @MAX INT
  20. DECLARE @dbname nvarchar(400)
  21. DECLARE @tbid INT
  22. DECLARE @tbl nvarchar(50)
  23. DECLARE @svr AS nvarchar(50)
  24.  
  25.  
  26.  
  27.  
  28. --set initial state of variables
  29. SET @i=1                                                                 --counter
  30. SET @max1 =(SELECT MAX(id) FROM #DBList)+1         --maximum value to iterate to
  31. SET @svr= UPPER(@@servername)                            --name of server
  32. SET @colID=1                                                        
  33.  
  34.  
  35.  
  36. ---gets a list of databases from server and populate #DBList table
  37.         while @i <@max1
  38.                 BEGIN
  39.                     SET @db = (SELECT DBname FROM #DBList WHERE @i=id)
  40.                     SET @SQL ='select '''+@db+''' ,id,name from '+ @db+'..sysobjects
  41.                                                                                 where xtype =''u'''
  42.                     SET @i=@i+1
  43.                     INSERT INTO #ColumnList(DbName,tbi, TableName)
  44.                     EXEC (@SQL)
  45. END
  46.  
  47. --get details based on column id
  48.     SET @MAX=(SELECT MAX(id) FROM #ColumnList)+1
  49.         while @colID < @MAX
  50.             BEGIN
  51.                 SET @dbname=(SELECT dbname FROM #ColumnList WHERE id=+ @colID)
  52.                 SET @tbid=(SELECT tbi FROM #ColumnList WHERE id=+ @colID)
  53.                 SET @tbl=(SELECT tablename FROM #ColumnList WHERE id=+ @colID)
  54.                 SET @sql1='select '''+@dbname +''','''+@tbl+''' ,name from ['+@dbname +']..syscolumns
  55.                 where id='+ CAST(@tbid AS nvarchar(20))
  56.                 INSERT INTO #TableList(dbname,Tablename,columnName)
  57.                 EXEC(@sql1)
  58.                 SET @colID=@colID+1
  59. END
  60.  
  61.  
  62.  
  63. --Select from Temp Table
  64. SELECT @svr AS Svr,DBName,TableName,ColumnName
  65. FROM #TableList
  66. ORDER BY DBname,tablename, columnname
  67.  
  68.  
  69. --CleanUp
  70. DROP TABLE #DBList
  71. DROP TABLE #TableList
  72. DROP TABLE #ColumnList

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.