declare @ varchar(8000)
set @=''
select @=@+rtrim(name)+' from t1 union all select ' from syscolumns where id=object_id('t1')
set @=left(@,len(@)-len(' from t1 union all select '))
--print @
exec('select '+@+' from t1')
--测试的表
select * into tb
from(
select aa='a',bb=15
union all select 'b',9
union all select 'c',1
union all select 'd',0
union all select 'e',1
union all select 'f',2
union all select 'g',2
union all select 'h',0
) a
--数据处理语句
declare @s varchar(8000)
set @s=''
select @s=@s+','+aa+'='+cast(bb as varchar) from tb
set @s=substring(@s,2,8000)
exec('select '+@s)
--测试的表
select a=15,b=9,c=1,d=0,e=1,f=2,g=2,h=0 into tb
--数据处理语句
declare @s varchar(8000)
set @s=''
select @s=@s+' union all select '+name+' from tb' from syscolumns where object_id('tb')=id
set @s=substring(@s,12,8000)
exec(@s)