请问在一个游标里怎么再套用一个游标,就像两个for循环语句一样,请给出示例代码.

解决方案 »

  1.   

    set @cr1 = cursor for
      select aa from tb_a
    open @cr1
    fetch next from @cr1 into @aa
    while @@fetch_status = 0
    begin
      set @cr2 = cursor for
        select bb form tb_b
      open @cr2 
      fetch next from @cr2 into @bb
      begin
        ....
        fetch next from @cr2 into @bb    
      end
      close @cr2
      deallocate @cr2
      .....
      fetch next from @cr1 into @aa
    end
    close @cr1
    deallocate @cr1
      

  2.   

    Sql server是一个集合操作语言;其操作对象是记录集,而游标是相对单条记录的操作;
    频繁使用会影响程序的运行速度。