不知道有人遇到过这样的问题没?当存储过程中有多条数据,要使用游标循环的时候,如果第一条数据没有满足条件就会跳出循环后面有符合条件数据的数据就没能循环做出在循环中相应的操作这样的问题该怎么解决呀?

解决方案 »

  1.   

    那为什么要跳出呢?第一笔不满足直接进入下一笔不行么?类似continue而不是break
      

  2.   


    declare @rece varchar(500)
          declare @MsgTitle varchar(200)
          declare @mmsLoc varchar(500)
     declare M cursor  --创建游标 M
    static 
     for select MsgTitle,MMSContentLocation from UV_MsgInbox t3 where t3.MsgType=1 and t3.MsgArrivedTime >= dateadd(minute,-5,getdate())  and t3.sender in (select 号码 from Result group by 号码)
    open M --打开游标
    fetch next from M into @MsgTitle,@mmsLoc
     set @mmsLoc=substring(@mmsLoc,0,charindex(',',@mmsLoc)) 
    while(@@fetch_status=0)
        begin
    declare sm cursor
      static
    for select a from dbo.fn_split(@receiver,',')
       open sm
    fetch next from sm into @rece
    while(@@fetch_status=0)
       begin
      if not exists(select  * from MSG_Sentbox where msgTitle=@sentContent and MMSContentLocation=@mmsLoc
                                      and Receiver= @str  and ActualSendTime >=dateadd(minute,-10,getdate()))
    insert into dbo.MSG_Outbox (Receiver,MsgType,MsgTitle,MMSContentLocation,SendTime)  values(@rece,1,@MsgTitle,@mmsLoc,getDate())
    fetch next from sm into @rece
       end
    close sm
    deallocate sm
    fetch next from M into @MsgTitle,@mmsLoc
              set @mmsLoc=substring(@mmsLoc,0,charindex(',',@mmsLoc)) 
        end 
    close M
    deallocate M
        end双重循环,就是在第二个循环,循环第一条数据的时候 (也就是我判断的是if not exists,其实循环第一条数据的时候是 exists的)就直接跳出循环了,后面还有几条数据都没有循环了 就直接停止执行整个存储过程了