我正在尝试执行返回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'
更新:游标已迭代,但仍显示错误消息“对变量dummycursor的无效引用”。
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;
您将需要遍历ref光标,并为其中的每一行打印出各个字段。在更新的版本中,您需要将游标读取到局部标量变量中,而不是另一个ref游标中:
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
注意,该variable
命令是不是在declare
块; 它是一个SQL Developer命令,而不是PL / SQL命令。由于是print
,虽然双方都只是在SQL * Plus的文档记录。还要注意在:result
块内开始的冒号,表明它是一个绑定变量,而不是本地PL / SQL变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句