Here is an example of the INFORMATION_SCHEMA.Columns table:
table_catalog table_schema table_name column_name
db1 dbo tblName1 colName1
db1 dbo tblName1 colName2
db1 dbo tblName2 colName1
db1 dbo tblName2 colName2
db1 dbo tblName2 colName3
the result I want (in a select statement) is:
table_name combined_columns
tblName1 colName1,colName2
tblName2 colName1,colName2,colName3
Now, I can do this on an individual level, setting a variable for each table:
table_catalog table_schema table_name column_name
db1 dbo tblName1 colName1
db1 dbo tblName1 colName2
db1 dbo tblName2 colName1
db1 dbo tblName2 colName2
db1 dbo tblName2 colName3
the result I want (in a select statement) is:
table_name combined_columns
tblName1 colName1,colName2
tblName2 colName1,colName2,colName3
Now, I can do this on an individual level, setting a variable for each table:
Code:
use pubs
declare @columnlist as varchar(8000)
declare @tblName as varchar(20)
set @tblName='titles'
SELECT @ColumnList = COALESCE(@ColumnList + ', ', '') + column_name
FROM INFORMATION_SCHEMA.Columns
WHERE table_name = @tblName
--and columnproperty(object_id(@tblName), column_name,'IsIdentity') <> 1
print @ColumnList
[\code]
This returns:
title_id, title, type, pub_id, price, advance, royalty, ytd_sales, notes, pubdate
But that is not what I want. I want:
tbl_name column_list
titles title_id, title, type, pub_id, price, etc....
stores stor_id, stor_name, stor_address, city, state, zip
Ideas?
Thanks.