游标可以嵌套吗?@@FETCH_STATUS是一个全局变量,如果两个游标嵌套的话,这个变量指示的是谁的状态呀?会不会乱呀?
--------------
由于业务复杂,想在存贮过程中定义两个游标,是嵌套的关系,即在一个循环中再定义一个游标,再进行循环,可是如何确定游标是否到最后呀,就要用@@FETCH_STATUS来判断,可是两个游标同时在循环,这个变量是否能用呢?会不会相互影响呀?

解决方案 »

  1.   

    IF EXISTS(SELECT name 
      FROM   sysobjects 
      WHERE  name = N'Place_OrderPrice' 
      AND   type = 'P')
        DROP PROCEDURE Place_OrderPrice
    GOCREATE  Procedure  Place_OrderPrice
    ASdeclare @gatheringid numeric(12)
    declare @notecode   varchar(12)
    declare @rederamount numeric(12,2)
    declare @updatedate_g  datetime
    declare @sendformid numeric(12)
    declare @sendformcode varchar(32)
    declare @sendformnumber numeric(12,2)
    declare @sendfromupdatedate   datetime
    declare @balance_num numeric(12,2)
    declare @balance_price numeric(12,2)
    declare @inum_pay numeric(12,2)begin
    declare cur1 cursor
    for SELECT gatheringid,notecode,rederamount,updatedate from gathering_info_t where isvalid='1' and rederamount>0 order by updatedate
    open cur1
    fetch next from cur1 INTO @gatheringid,@notecode,@rederamount,@updatedate_gwhile (@@fetch_status=0  )
        begin
            --取开票信息
            declare cur2 cursor
            for SELECT sendformid,sendformcode,sendformnumber,updatedate from sendform_info_t where isvalid='1'  order by updatedate
            open cur2
            fetch next from cur2 INTO @sendformid,@sendformcode,@sendformnumber,@sendfromupdatedate        while (@@fetch_status=0 and @rederamount>0)
                       begin
    --取本张发票已结算数量
    select @inum_pay=isnull(sum(balancenumber),0)
    from place_orderprice_t
    where sendformid=@sendformid
    set @balance_num=@balance_num-@inum_pay                       select @balance_price=orderprice from old_orderprice_t where startdate<=@updatedate_g and @updatedate_g<enddate
                           if @rederamount/@balance_price>=@sendformnumber 
                               begin
                                   set @balance_num=@sendformnumber
                               end
                           else
                               begin
                                   set @balance_num=@rederamount/@balance_price
                               end
    if @balance_num>0
    begin
                           insert into place_orderprice_t(sendformid,balancenumber,gatherid,orderprice) values(@sendformid,@balance_num,@gatheringid,@balance_price)
                    ---update gathering_info_t set rederamount=rederamount-(@balance_price*@balance_num)
                            set @rederamount=@rederamount-(@balance_price*@balance_num)
       
    end    

                   fetch next from cur2 into @sendformid,@sendformcode,@sendformnumber,@sendfromupdatedate
    end
            close cur2
            deallocate cur2
            fetch next from cur1 INTO @gatheringid,@notecode,@rederamount,@updatedate_g
        endclose cur1
    deallocate cur1
    print '执行完毕'
    end--这是我当时写的,你注意查看
      

  2.   

    这么说,@@FETCH_STATUS不应称为全局变量呀?
    它是属于某一个游标的,是吗?
      

  3.   

    这么说,@@FETCH_STATUS不应称为全局变量呀?
    它是属于某一个游标的,是吗?
    -------------------------------------------------------------------------------------
    不是.@@FETCH_STATUS属于任何游标的,只要任何一个游标被提取了,这个提取成功与否的状态就会保存到@@FETCH_STATUS中.
    嵌套游标的原理类似这样:
    declare 外层游标
    open 外层游标
    fetch next ...提取外层游标行
    while @@FETCH_STATUS = 0
    begin
        declare 内层游标
        open 内层游标
        ftech next ...提取内层游标行
        while @@FETCH_STATUS = 0
        begin
              .....处理内层游标
              ftech next ....内层游标向下移动一行
        end
        close 内层游标
        deallocate 内层游标
        fetch next ....内层游标处理结束后,外层游标才继续向下移动一行
    end 
    close 外层游标
    deallocate 外层游标
    也就是说,外层游标每移动一行,就要重复进行内层游标定义,打开,循环,关闭,释放等操作,然后才能再向下移动行.
    具体请楼主参考DECLARE CURSOR,里面有个嵌套游标的例子,很容易理解.
      

  4.   

    可以嵌套,我刚做过一个这样的SP@@fetch_status的值是最后一次fetch 语句的返回值。