当前位置: 代码迷 >> Sql Server >> 问一个循环取值的有关问题
  详细解决方案

问一个循环取值的有关问题

热度:30   发布时间:2016-04-25 00:45:21.0
问一个循环取值的问题.
取值的数量是一个变量,目前遇到的问题时,如果这样执行,会产生多个结果窗口,有没有办法能把这些结果union all在一起?
大概简化的逻辑和下面差不多

set @i=1
while @i<=@xxx
begin
with a as(select...)
select * from a 
set @i=@i+1
end

------最佳解决方案--------------------
declare @TB table(....)
set @i=1
 while @i<=@xxx
 begin
 with a as(select...)
 insert into @TB(...)
 select * from a 
 set @i=@i+1
 end
 select * from @TB
------其他解决方案--------------------

set @i=1
while @i<=@xxx
begin
with a as(select...)
set @i=@i+1
end
select * from a 

没有测试数据不知道你想要什么的结果,上面的写法只能确保你最后一次查出来的数据,而不是多个查询窗口
------其他解决方案--------------------
没看明白,lz给个数据结构和结果看看?

------其他解决方案--------------------
把循环体中的内容放到一个临时表
------其他解决方案--------------------
这样呢?

set @i=1
while @i<=@xxx
begin
with a as(select...)
select * into #temp from a 
set @i=@i+1
end
select * from #temp
  相关解决方案