问题:在查看老代码的时发现同一个cur第一次使用后没关闭,又第二次使用,程序使用了
很久,一直没有发现问题,至少应用层是没有出问题。那到底oracle会不会抛出异常?
测试:
(A)
create or replace procedure proc_test
as
type cursor_type is ref cursor;
cur cursor_type;
v_begin date;
v_end date;
begin
open cur
for
select sysdate from dual;
fetch cur into v_begin;
open cur for
----再次open
select sysdate from dual;
fetch cur into v_end;
insert into
proc_use_times(PROC_NAME,PROC_DESC,BEGIN_TIME,END_TIME)
values('测试','测试',v_begin,v_end);
commit;
----而且没有关闭
exception
when others then
raise_application_error(-20123,sqlcode||sqlerrm);
end;
begin
proc_test;
end;
/
select *
from proc_use_times p
where p.PROC_NAME='测试'
----得出一条记录并且没有抛出异常 答案是:A种情况下不会报错。
(B)
create or replace procedure proc_test
as
type cursor_type is ref cursor;
cur cursor_type;
v_begin date;
v_end date;
begin
open cur
for
select sysdate from dual;
fetch cur into v_begin;
open cur
for
select sysdate from dual;
fetch cur into v_end;
insert into
proc_use_times(PROC_NAME,PROC_DESC,BEGIN_TIME,END_TIME)
values('测试','测试',v_begin,v_end);
commit;
close
cur;----使用两次并且最后一次关闭
exception
when others then
raise_application_error(-20123,sqlcode||sqlerrm);
end
-------------答案同A 依然没有报错
(C)
create or replace procedure
proc_test
as
type cursor_type is ref cursor;
cur cursor_type;
v_begin date;
v_end date;
begin
open cur for
----第一次打开
select sysdate from dual;
fetch cur into v_begin;
open cur for
---第二次打开
select sysdate from dual;
fetch cur into v_end;
insert into
proc_use_times(PROC_NAME,PROC_DESC,BEGIN_TIME,END_TIME)
values('测试','测试',v_begin,v_end);
commit;
close
cur;----第一次关闭
close cur;----第二次关闭
exception
when others then
raise_application_error(-20123,sqlcode||sqlerrm);
end
-----------答案是出错了。报了异常,从A和B两个测试的基础上,不难发现,C的报错也是合情合理的。
原因是既然已经关闭了,那再次关闭的时候 就会发现找不到这个游标了@@