| re: List of columns from tables across databases.
Query other databases using the three-part name:
SELECT *
FROM database_name.information_schema.columns
SELECT C.name, O.name, *
FROM database_name.dbo.syscolumns AS C,
database_name.dbo.sysobjects AS O
WHERE O.id = C.id
AND (O.xtype='U' OR O.xtype='S')
Assuming you have set up a linked server you can query other servers with
the four-part name:
SELECT *
FROM server_name.database_name.information_schema.colum ns
SELECT C.name, O.name, *
FROM server_name.database_name.dbo.syscolumns AS C,
server_name.database_name.dbo.sysobjects AS O
WHERE O.id = C.id
AND (O.xtype='U' OR O.xtype='S')
In each case the tables are distinct objects so if you want to combine
results from multiple databases either use a UNION or write a loop that
cycles through each DB. There is actually an undocumented proc that will
access each DB in turn:
EXEC sp_msforeachdb 'USE ? SELECT DB_NAME()'
This is something you should avoid in persistent code because it won't
necessarily be supported in future but it may help you if this is just a
one-off exercise.
--
David Portas
SQL Server MVP
-- |