September 28, 2010 at 10:55 am
Hello All,
I have an application that uses a back end SQL Server 2005 DB. The application creates 100+ can tables and views during the installatiion. Whenever information is requested, I have to go through each table or view to determine if the requested information is there or at least a column that sounds like it contains the requested data.
I know I can go through sys.tables or sys.views to get a list of all the tables/views in the DB. Is there a way I can query each table for a list of columns? I would want to query all tables/views and save the output in a spreadsheet or CSV file with a header of the table or view name.
Is there a tool or query that anybody knows of that could be used for this?
Thanks in advance for your help!!
Ronnie
September 28, 2010 at 11:17 am
one of the companion views is sys.columns; so you can join sys.tables (for jsut tables) or sys.objects if you want views and tables:
select
tab.name as TableName,
col.name as ColumnName,
col.column_id as ColumnID
FROM sys.objects tab
LEFT OUTER JOIN sys.columns col
ON tab.object_id = col.object_id
WHERE tab.type IN ('U','V')
ORDER BY tab.name,col.column_id
Lowell
September 28, 2010 at 11:18 am
This query will produce the table name, and a comma-separated list of columns in that table.
SELECT DISTINCT
t.name,
sq.Columns
FROM sys.tables t
JOIN (
SELECT OBJECT_ID,
Columns = STUFF((SELECT ',' + name
FROM sys.columns sc
WHERE sc.object_id = s.object_id
FOR XML PATH('')),1,1,'')
FROM sys.columns s
) sq ON t.object_id = sq.object_id
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 29, 2010 at 8:10 am
Thanks Lowell for the query. It works great. I really appreciate your help!
Ronnie
September 29, 2010 at 8:14 am
Wayne - Thank you for the query. It works great. I plan to research the "FOR XML PATH" statement just to see the use of it. One way for me to learn.
thanks again!
Ronnie
September 29, 2010 at 8:28 am
Ronnie Jones (9/29/2010)
Wayne - Thank you for the query. It works great. I plan to research the "FOR XML PATH" statement just to see the use of it. One way for me to learn.thanks again!
Ronnie
Well, let me just explain a few things for you that might be hard so find...
1. the column created (',' + name) produces an unnamed column, so there won't be a column token around it.
2. the FOR XML PATH('') specifies not to use a token around the whole string.
3. the stuff function removes the leading comma
I see that I forgot the order by clause; here is corrected code:
SELECT DISTINCT
t.name,
sq.Columns
FROM sys.tables t
JOIN (
SELECT OBJECT_ID,
Columns = STUFF((SELECT ',' + name
FROM sys.columns sc
WHERE sc.object_id = s.object_id
ORDER BY sc.column_id
FOR XML PATH('')),1,1,'')
FROM sys.columns s
) sq ON t.object_id = sq.object_id
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
September 29, 2010 at 8:30 am
This is probably the easiest, and it works with all versions of SQL Server from 7.0 on:
select * from INFORMATION_SCHEMA.COLUMNS
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply