我有一个SQL Server游标.我想在特定条件发生时跳过循环的一次迭代.Break会将您带出游标循环并继续执行任何操作.
是否有一个命令说"嘿,这个记录不好,所以继续前进,跳过它,继续下一个".
顺便说一下,我知道游标是邪恶的,就像驾驶员在过往车道上的43英里/小时,但是经常发生在软件中,我坚持使用它.
谢谢
KM.*_*KM. 11
如果您使用底部的fetch编写循环(在循环之前使用初始提取)继续将跳转到顶部,并再次处理同一行.您可以使用GOTO跳转到底部的获取部分,或者重新构造循环以获取顶部,并且cointinue将起作用.
你可以修改你的循环使用GOTO ......
...
...
if <condition>
BEGIN
GOTO Fetch_Next
END
....
....
Fetch_Next:
FETCH NEXT FROM ...
Run Code Online (Sandbox Code Playgroud)
下面是循环顶部只有一次提取的示例代码,继续工作:
DECLARE <cursor_name> CURSOR FOR
SELECT
FROM
WHERE
FOR READ ONLY
--populate and allocate resources to the cursor
OPEN <cursor_name>
--process each row
WHILE 1=1
BEGIN
FETCH NEXT FROM <cursor_name>
INTO @a, @b, @c
--finished fetching all rows?
IF @@FETCH_STATUS <> 0
BEGIN --YES, all done fetching
--exit the loop
BREAK
END --IF finished fetching
--do something here--
--do something here--
IF <your condition>
BEGIN
CONTINUE -- fetch next row
END
--do something here--
--do something here--
END --WHILE
--close and free the cursor's resources
CLOSE <cursor_name>
DEALLOCATE <cursor_name>
Run Code Online (Sandbox Code Playgroud)
为什么不使用if语句:
IF 'condition exists that I want to update'
BEGIN
....
END
Fetch Next
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17186 次 |
最近记录: |