Friday, June 25, 2010

Listing Table Column Names On One Line

I wanted to list out all the names of a table on one line, separated by tab characters, so that I could do a simple copy-paste from the SQL Server Management Studio into an Excel sheet - having each column name place itself conveniently into the next Excel column.

The code I came up with is a slight modification of a previous post:
select STUFF(
(SELECT char(9) + name FROM
(SELECT c.name
FROM syscolumns c inner join sysobjects o on c.id = o.id
where o.name = 'your-table-name') AS Y
--ORDER BY names -- optional sorting of the column names
FOR XML PATH('')),
1, 1, N'')



No comments:

Post a Comment