Adsense

Friday, May 1, 2015

Create a Comma Separated Column List for a SQL Server Table

declare @t as varchar(100)
declare @all as varchar(4000)

declare curs cursor for
select rtrim(c.name) + ', '
from sysobjects o
inner join syscolumns c on o.id = c.id
where o.xtype = 'u'
and o.name = 'table_name'
order by colid

open curs

fetch next from curs into @t

set @all = ''

while @@fetch_status = 0
begin

       set @all = @all + @t

       fetch next from curs into @t

end

close curs

deallocate curs

print @all


No comments:

Post a Comment