我的理解是,在调用过程时,形式参数应该始终默认为NULL。
create or replace package parameter_tests as
procedure callerproc;
end parameter_tests;
/
create or replace package body parameter_tests as
procedure getstring(p_str out varchar) is
begin
if p_str is null then
dbms_output.put_line('parameter null');
else
dbms_output.put_line('parameter NOT null');
end if;
p_str :='zz';
end getstring;
procedure getcursor(p_out out sys_refcursor) is
begin
if p_out%isopen then
dbms_output.put_line('cursor open');
else
dbms_output.put_line('cursor closed');
end if;
open p_out for
select *
from dual;
end getcursor;
procedure callerproc is
lv_cursor sys_refcursor;
lv_string varchar2(2) := null;
begin
for i in 1..2 loop
getstring(lv_string);
getcursor(lv_cursor);
end loop;
end callerproc;
end parameter_tests;
/
set serveroutput on
begin
parameter_tests.CALLERPROC;
end;
/parameter_tests.getstring希望在callerproc循环中两次输出"Parameter“。当你运行代码时,情况就是这样。
但是,parameter_tests.getcursor的输出表明引用游标在循环中第二次打开。
如果在调用cursor游标时形式参数p_out为空,我们希望它关闭引用游标。相反,它传递打开的引用游标,它实际上是打开的,清理打开的引用游标(如果您循环数百次,我们就不会碰到最大的打开游标)。
如果我手动取消调用之间的引用游标,那么它的行为就像我们所期望的那样。
有人能告诉我为什么引用游标是作为特例来处理的吗?另外,还有哪些其他类型的处理方式不同?
数据库版本为11.2.0.2.0。
发布于 2017-04-20 17:33:35
将游标变量声明为子程序的形式参数时:
游标参数似乎被视为已声明为IN,即使实际上只将其声明为OUT。这种行为是您期望从IN中看到的;您甚至可以在第二个调用中获取游标,并从dual中看到虚拟值。
考虑到ref游标作为指针的性质,这有点道理,但您可能会认为,如果只指定OUT (甚至没有报告为警告),则该规则将由编译器执行错误。顺便说一句,docs中的示例也适用于OUT。
因此,这看起来确实是一个编译器错误,因为它没有报告不正确的参数方向;而且(更顽强地!)在您的代码中有一个bug,因为它没有被声明为in。还有可能是另一个bug,因为您没有显式关闭游标--这似乎也“修复”了这个问题,有点像:
procedure callerproc is
lv_cursor sys_refcursor;
lv_string varchar2(20) := null;
begin
for i in 1..2 loop
getstring(lv_string);
getcursor(lv_cursor);
close lv_cursor;
end loop;
end callerproc;这可能比将null赋值给游标变量更正确,您还提到了这一点。
https://stackoverflow.com/questions/43525291
复制相似问题