日期:2014-05-17 浏览次数:20525 次
create proc p_test(@column int)
as
begin
declare @sql nvarchar(max),@num int
set @num=1
set @sql='declare @t table(id int,a int,b int'
while @num<=@column begin
set @sql=@sql+',a'+convert(nvarchar(10),@num)+' int'
set @num=@num+1
end
set @sql=@sql+')'
set @sql=@sql+' insert into @t(id,a,b) select id,a,b from tab '
set @sql=@sql+' select * from @t'
exec(@sql)
end
create proc xiaohao
@n varchar(10)
as
begin
declare @s varchar(255)
declare @i int
set @i=1
set @s='select id,a,b'
while @i<=cast(@n as int)
begin
set @s=@s+','+char(39)+char(39)+' as a'+cast(@i as varchar(5))
set @i=@i+1
end
set @s=@s+' from tab'
exec (@s)
end