在Oracle SQL Developer 1.5中打印Oracle Sys_refcursor

San*_*ana 4 oracle plsql cursor oracle-sqldeveloper

我正在尝试执行返回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;
Run Code Online (Sandbox Code Playgroud)

错误是

PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
Run Code Online (Sandbox Code Playgroud)

更新:迭代了光标,但仍然得到错误为"对变量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;
Run Code Online (Sandbox Code Playgroud)

Ale*_*ole 13

您需要遍历引用游标,并为其中的每一行打印出各个字段.在更新的版本中,您需要将光标提取到本地标量变量,而不是另一个引用光标:

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;
/
Run Code Online (Sandbox Code Playgroud)

我猜想lsn是一个数字,如果没有,那么声明它是正确的类型.如果游标返回多个列,那么您将需要为每个列声明局部变量并将它们全部提取到那些中,即使您只显示其中一个.


如果您只想显示它,那么您可以使用绑定变量来执行此操作(在当前版本中检查并返回到1.5.0):

variable result refcursor

begin
  emp.emp360_utils.GET_EMPLOYEEs(222334, :result); 
end;
/

print result
Run Code Online (Sandbox Code Playgroud)

注意,variable命令不是declare块; 它是SQL Developer命令,而不是PL/SQL命令.由于是print,虽然双方都只是在SQL*Plus的文档记录.还要注意:result块内开头的冒号,表示它是绑定变量,而不是本地PL/SQL变量.