22,302
社区成员




declare @s varchar(max)='';
with t(id,txt) as (
select 1,'select ''xxx'' ' txt union all
select 2,'select ''yyy'' ' txt union all
select 3,'select ''zzz'' '
)
select @s=@s+' union all select '+convert(varchar(50),t.id)+' id,('+txt+')rst' from t
set @s=SUBSTRING(@s,11,LEN(@s))
print @s;
exec(@s);
但5w行的数据拼那么长串,是否会遇到什么限制就不知道了。如果有限制,你只能拆成每N个拼个串试试。再把多个结果集,和到一起再输出试试。
--用循环代替游标:
select i=identity(int,1,1),ID,PROID='' into #t from tb
declare @sql varchar(4000),@txt varchar(4000),@i int,@n int
select @n=MAX(i) from #t
while(@i<@n)
begin
select @txt=a.SQL_TEXT from tb a,#t b where a.ID=b.ID and b.i=@i
set @sql='update #t set PROID=('+@txt+') where i='+rtrim(@i)
exec(@sql)
set @i=@i+1
end
select a.ID,a.TYPE,b.PROID
from tb a,#t b
where a.ID=b.ID