PLSQL "error Cursor already open"
PLSQL "error Cursor already open"
我有以下代码:
DECLARE
f_cd fee.fee_cd%type;
f_name fee.fee_nm%type;
f_new_cd fee.new_fee_cd%type;
f_new_name fee.new_fee_nm%type;
Cursor cur is
SELECT Fee_cd, fee_nm, new_fee_cd, new_fee_nm FROM Fee;
BEGIN
if cur%ISOPEN then
close cur;
end if;
open cur;
for rec in cur loop
fetch cur INTO f_cd, f_name, f_new_cd, f_new_name;
dbms_output.put_line ('The Fee Code ' || f_cd
|| ' is the one you selected and it''s name is '
|| f_name);
end loop;
close cur;
END;
但我一直收到错误消息
原因:试图打开一个已经打开的游标。
操作: 重新打开前先关闭游标。
我不知道发生了什么。当我更改代码以删除 for loop
并仅使用 loop... end loop
结构时,它起作用了。下面的功能代码:
loop
fetch cur INTO f_cd, f_name, f_new_cd, f_new_name;
dbms_output.put_line ('The Fee Code ' || f_cd
|| ' is the one you selected and it''s name is '
|| f_name);
exit when cur%notfound;
end loop;
close cur;
END;
为什么当我使用 for 循环时它告诉我游标已经打开?
您正在打开游标:
open cur;
然后在不关闭它的情况下,您在游标循环中再次打开:
for rec in cur loop
"for cursor loop" 构造首先打开游标。无需事先打开它。见documentation:
"The cursor FOR LOOP statement implicitly declares its loop index as a record variable of the row type that a specified cursor returns, and then opens a cursor. "
两种使用方式 Cursor
:
OPEN
; FETCH INTO
;CLOSE
;
FOR I IN C1
;
方法一: OPEN C1
; LOOP
; FETCH C1 INTO
; END LOOP
; CLOSE C1
;
DECLARE
v_code_id your_users.cod_id%type;
v_code_user your_users.cod_user%type ;
cursor C_users is select cod_id,cod_user from your_users where 1=1;
BEGIN
OPEN C_users; --opening cursor
loop
Fetch C_users into v_code_id,v_code_user; -- Fetching from Cursoe
exit when C_users%NOTFOUND;
DELETE from your_users WHERE cod_emp IN (v_code_id);
dbms_output.put_line( 'USER : ' || ' ' || v_code_user || ' is deleted.' );
End Loop;
commit;
Close C_users ; --Closing Cursor
END;
输出:
USER : mahi is deleted.
USER : xyz is deleted.
Statement processed.
方法二: FOR i in C1
; LOOP
; END LOOP
DECLARE
cursor C_users is
select cod_id,cod_user from your_users where 1=1;
BEGIN
For rec in C_users
loop
DELETE from your_users WHERE cod_emp IN (rec.cod_id );
dbms_output.put_line( 'USER : ' || ' ' || rec.cod_user || ' is deleted.' );
End Loop;
commit;
END;
输出:
USER : xyz is deleted.
USER : mahi is deleted.
我有以下代码:
DECLARE
f_cd fee.fee_cd%type;
f_name fee.fee_nm%type;
f_new_cd fee.new_fee_cd%type;
f_new_name fee.new_fee_nm%type;
Cursor cur is
SELECT Fee_cd, fee_nm, new_fee_cd, new_fee_nm FROM Fee;
BEGIN
if cur%ISOPEN then
close cur;
end if;
open cur;
for rec in cur loop
fetch cur INTO f_cd, f_name, f_new_cd, f_new_name;
dbms_output.put_line ('The Fee Code ' || f_cd
|| ' is the one you selected and it''s name is '
|| f_name);
end loop;
close cur;
END;
但我一直收到错误消息
原因:试图打开一个已经打开的游标。
操作: 重新打开前先关闭游标。
我不知道发生了什么。当我更改代码以删除 for loop
并仅使用 loop... end loop
结构时,它起作用了。下面的功能代码:
loop
fetch cur INTO f_cd, f_name, f_new_cd, f_new_name;
dbms_output.put_line ('The Fee Code ' || f_cd
|| ' is the one you selected and it''s name is '
|| f_name);
exit when cur%notfound;
end loop;
close cur;
END;
为什么当我使用 for 循环时它告诉我游标已经打开?
您正在打开游标:
open cur;
然后在不关闭它的情况下,您在游标循环中再次打开:
for rec in cur loop
"for cursor loop" 构造首先打开游标。无需事先打开它。见documentation:
"The cursor FOR LOOP statement implicitly declares its loop index as a record variable of the row type that a specified cursor returns, and then opens a cursor. "
两种使用方式 Cursor
:
OPEN
;FETCH INTO
;CLOSE
;FOR I IN C1
;
方法一: OPEN C1
; LOOP
; FETCH C1 INTO
; END LOOP
; CLOSE C1
;
DECLARE
v_code_id your_users.cod_id%type;
v_code_user your_users.cod_user%type ;
cursor C_users is select cod_id,cod_user from your_users where 1=1;
BEGIN
OPEN C_users; --opening cursor
loop
Fetch C_users into v_code_id,v_code_user; -- Fetching from Cursoe
exit when C_users%NOTFOUND;
DELETE from your_users WHERE cod_emp IN (v_code_id);
dbms_output.put_line( 'USER : ' || ' ' || v_code_user || ' is deleted.' );
End Loop;
commit;
Close C_users ; --Closing Cursor
END;
输出:
USER : mahi is deleted.
USER : xyz is deleted.
Statement processed.
方法二: FOR i in C1
; LOOP
; END LOOP
DECLARE
cursor C_users is
select cod_id,cod_user from your_users where 1=1;
BEGIN
For rec in C_users
loop
DELETE from your_users WHERE cod_emp IN (rec.cod_id );
dbms_output.put_line( 'USER : ' || ' ' || rec.cod_user || ' is deleted.' );
End Loop;
commit;
END;
输出:
USER : xyz is deleted.
USER : mahi is deleted.