Technical Article

Column Listing

,

Gets a list of columns for a given table.

CREATE proc sp_get_column_list
 @table_name varchar(128)
as
declare @col_list varchar (1024)
declare @max_col int
declare @table_id int
declare @this_col int
select @table_id = id from sysobjects 
where name = @table_name
select @max_col = max(colid) from syscolumns
where id = @table_id
select @this_col = min(colid) from syscolumns
where id = @table_id
set @col_list = ''
while @this_col != @max_col
begin
select @col_list = @col_list + ' ' + name + ',' from syscolumns
where id = @table_id
and colid = @this_col
set @this_col = @this_col + 1
end
--last col doesn't get a comma
select @col_list = @col_list + ' ' + name from syscolumns
where id = @table_id
and colid = @this_col
print @col_list

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating