Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Oracle Sys_refcursor in Oracle SQL Developer 1.5

I am trying to execute the procedure which returns a sys_refcursor as output. The procedure is PROCEDURE GET_EMPLOYEEs(P_ID in NUMBER, P_OUT_CURSOR OUT SYS_REFCURSOR);

I wrote the below anonymous block in SQL Developer 1.5 and its executing fine,but when I try to print the cursor, I am getting an error. The cursor returns emp_name,salary and other columns.

set serveroutput on;
declare
result sys_refcursor;
begin
emp.emp360_utils.GET_EMPLOYEEs(222334,result); 
dbms_output.put_line(result); // Error here
end;

The error is

PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'

UPDATED: Iterated for cursor,but still getting error as "Invalid reference to variable 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;
like image 952
Sanjana Avatar asked Jul 04 '14 08:07

Sanjana


People also ask

What is Sys_refcursor in Oracle PL SQL?

SYS_REFCURSOR. Specifies that the data type of the cursor variable is the built-in SYS_REFCURSOR data type.

What is the difference between ref cursor and Sys_refcursor?

There is no difference between using a type declared as REF CURSOR and using SYS_REFCURSOR , because SYS_REFCURSOR is defined in the STANDARD package as a REF CURSOR in the same way that we declared the type ref_cursor .

Is ref cursor PL SQL Oracle?

A REF CURSOR is a PL/SQL data type whose value is the memory address of a query work area on the database. In essence, a REF CURSOR is a pointer or a handle to a result set on the database. REF CURSOR s are represented through the OracleRefCursor ODP.NET class.


2 Answers

You would need to loop over the ref cursor and for each row in it, print out the individual fields. In your updated version you need to fetch the cursor into local scalar variables, not another ref cursor:

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;
/

I've guessed lsn is a number, if not then declare that as the right type. If the cursor returns more than one column then you will need to declare local variables for each of them and fetch them all into those, even if you're only displaying one of them.


If you just want to display it then you can use a bind variable to do this instead (checked in the current version and back to 1.5.0):

variable result refcursor

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

print result

Note that the variable command is not in the declare block; it is a SQL Developer command, not a PL/SQL command. As is print, though both are only documented in the SQL*Plus docs. And also note the colon at the start of :result within the block, which indicates that it is a bind variable, not a local PL/SQL variable.

like image 134
Alex Poole Avatar answered Oct 20 '22 00:10

Alex Poole


You can execute procedure using Run button in package source enter image description here

and view cursor content in tab Output variables enter image description here

like image 42
Alex78191 Avatar answered Oct 20 '22 02:10

Alex78191