Tuesday, August 7, 2012

Delimited List of Columns

I frequently need to get a list of columns of a database table in a delimited format, and have found myself rediscovering the following query:

select '['+ [name] + '],'

from sys.columns

where object_name(object_id) = 'table-name'

order by column_id

for xml path('')


For a table with three columns, the return from the above would be:

[Column1],[Column2],[Column3],

Enjoy!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.