@@FETCH_STATUS 返回被 FETCH 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。返回值 描述 0 FETCH 语句成功。 -1 FETCH 语句失败或此行不在结果集中。 -2 被提取的行不存在。 WHILE @@FETCH_STATUS = 0 BEGIN do ...... fetch ...... END
/* 这要看游标的嵌套怎么写,下面的写法,就不会出现这样的问题: */declare cursor_001 cursor for ... open cursor_001 fetch next from cursor_001 into ... -- 当 @@fetch_status = 0 while @@fetch_status = 0 begin declare cursor_002 cursor for ... open cursor_002 fetch next from cursor_002 into ... while @@fetch_status = 0 begin ... fetch next from cursor_002 into ... end close cursor_002 deallocate cursor_002 -- 内层游标结束,@@fetch_status <> 0 fetch next from cursor_001 into ... -- 外层游标的 @@fetch_status,并不受内层的影响。 end close cursor_001 deallocate cursor_001
返回被 FETCH 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。返回值 描述
0 FETCH 语句成功。
-1 FETCH 语句失败或此行不在结果集中。
-2 被提取的行不存在。
WHILE @@FETCH_STATUS = 0
BEGIN
do ......
fetch ......
END
这要看游标的嵌套怎么写,下面的写法,就不会出现这样的问题:
*/declare cursor_001 cursor for ...
open cursor_001
fetch next from cursor_001 into ...
-- 当 @@fetch_status = 0
while @@fetch_status = 0
begin
declare cursor_002 cursor for ...
open cursor_002
fetch next from cursor_002 into ...
while @@fetch_status = 0
begin
...
fetch next from cursor_002 into ...
end
close cursor_002
deallocate cursor_002
-- 内层游标结束,@@fetch_status <> 0
fetch next from cursor_001 into ...
-- 外层游标的 @@fetch_status,并不受内层的影响。
end
close cursor_001
deallocate cursor_001