当前位置: 代码迷 >> Sql Server >> 如何跳出游标循环
  详细解决方案

如何跳出游标循环

热度:72   发布时间:2016-04-27 12:11:10.0
怎么跳出游标循环?
declare AutoAudit_cursor cursor for
select AuditHistoryId,FlowId
from AuditingHistory 
where [email protected]
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 [email protected]
open AutoAudit_cursor
fetch next from AutoAudit_cursor
into @AuditHistoryId,@FlowId
……

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

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