取值的数量是一个变量,目前遇到的问题时,如果这样执行,会产生多个结果窗口,有没有办法能把这些结果union all在一起?
大概简化的逻辑和下面差不多set @i=1
while @i<=@xxx
begin
with a as(select...)
select * from a 
set @i=@i+1
end

解决方案 »

  1.   

    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
      

  2.   


    set @i=1
    while @i<=@xxx
    begin
    with a as(select...)
    set @i=@i+1
    end
    select * from a 没有测试数据不知道你想要什么的结果,上面的写法只能确保你最后一次查出来的数据,而不是多个查询窗口
      

  3.   

    这样呢?set @i=1
    while @i<=@xxx
    begin
    with a as(select...)
    select * into #temp from a 
    set @i=@i+1
    end
    select * from #temp