Technical Article

Get a CSV list of columns in a table

,

Just put the table name in the @TableName variable.

You can also apply filtering in the where clause to only get certian fields.

DECLARE @TableName SYSNAME = 'tablename'

SELECT REPLACE(REVERSE(STUFF(REVERSE((SELECT name + ',' AS [data()]
FROM sys.columns
WHERE OBJECT_NAME(object_id) = @TableName
ORDER BY column_id
FOR XML PATH(''))),1,1,'')),' ','')

Rate

4 (5)

You rated this post out of 5. Change rating

Share

Share

Rate

4 (5)

You rated this post out of 5. Change rating