Retrieve column names from a table as a string

SQL Add comments

DECLARE @c varchar(4000), @t varchar(128)
SET @c = ”SET @t=’authors’
SELECT @c = @c + c.name + ‘, ‘ FROM syscolumns c INNER JOIN sysobjects o ON o.id = c.id WHERE o.name = @t ORDER BY colid
SELECT Substring(@c, 1, Datalength(@c) – 2)

(you can replace this value ‘, ‘ above with any delimiter you choose like ‘-‘ or ‘;’ then update the -2 in the last line with the number of characters in your delimiter)

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in