日期:2014-05-18  浏览次数:20721 次

怎么跳出游标循环?
declare AutoAudit_cursor cursor for
select AuditHistoryId,FlowId
from AuditingHistory 
where BillId=@BillId
open AutoAudit_cursor
fetch next from AutoAudit_cursor
into @AuditHistoryId,@FlowId
while @@FETCH_STATUS<>-1
begin
   
  exec(dosomething……)

fetch next from AutoAudit_cursor
into @AuditHistoryId,@FlowId
end

CLOSE AutoAudit_cursor
DEALLOCATE AutoAudit_cursor

循环里面有很多行,执行用exec来执行,想问一下当达到条件之后怎么终止游标循环


------解决方案--------------------
探讨
declare AutoAudit_cursor cursor for
select AuditHistoryId,FlowId
from AuditingHistory
where BillId=@BillId
open AutoAudit_cursor
fetch next from AutoAudit_cursor
into @AuditHistoryId,@FlowId
……

------解决方案--------------------
当达到条件之后,可以在 while 里面加上 Break 语句跳出循环。
另外建议while 的条件改为 @@FETCH_STATUS=0;

最后建议如果能不用游标就尽量不要用游标,尽量想想其他的解决办法,
你可以把问题发到论坛,看有没有更好的解决办法。