我正在尝试执行返回 sys_refcursor 作为输出的过程。程序是PROCEDURE GET_EMPLOYEEs(P_ID in NUMBER, P_OUT_CURSOR OUT SYS_REFCURSOR);
我在 SQL Developer 1.5 中编写了下面的匿名块,它执行得很好,但是当我尝试打印光标时,出现错误。游标返回emp_name、salary等列。
set serveroutput on;
declare
result sys_refcursor;
begin
emp.emp360_utils.GET_EMPLOYEEs(222334,result);
dbms_output.put_line(result); // Error here
end;
错误是
PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
更新:对光标进行迭代,但仍然收到错误“对变量虚拟光标的引用无效”。
set serveroutput on;
declare
dummycursor sys_refcursor;
result sys_refcursor;
begin
emp.emp360_utils.GET_EMPLOYEEs(222334,result);
LOOP
fetch result into dummycursor;
EXIT when result%notfound;
dbms_output.putline(dummycursor.lsn);
end loop;
end;
您需要循环引用游标并为其中的每一行打印出各个字段。在更新的版本中,您需要将光标提取到本地标量变量中,而不是另一个引用光标:
set serveroutput on;
declare
result sys_refcursor;
lsn number; -- guessing the data type
begin
emp.emp360_utils.GET_EMPLOYEEs(222334,result);
loop
fetch result into lsn; -- and other columns if needed
exit when result%notfound;
dbms_output.put_line(lsn);
end loop;
end;
/
我已经猜到了lsn
是一个数字,如果不是,则将其声明为正确的类型。如果光标返回多于一列,那么您将需要为每一列声明局部变量并将它们全部提取到其中,即使您只显示其中一列。
如果您只想显示它,那么您可以使用绑定变量来执行此操作(在当前版本中检查并返回到 1.5.0):
variable result refcursor
begin
emp.emp360_utils.GET_EMPLOYEEs(222334, :result);
end;
/
print result
注意the variable command is not in the declare
堵塞;它是 SQL Developer 命令,而不是 PL/SQL 命令。As is print,尽管两者仅记录在 SQL*Plus 文档中。还要注意开头的冒号:result
在块内,这表明它是一个绑定变量,而不是本地 PL/SQL 变量。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)