Skip to Main Content
  • Questions
  • Using SYS.REFCURSOR out variable as a source for classic report in ORACLE APEX.

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Reshu.

Asked: August 16, 2016 - 4:28 pm UTC

Last updated: August 20, 2016 - 3:21 am UTC

Version: ORACLE APEX 5

Viewed 1000+ times

You Asked

Hi ,
I have a urgent requirement, I have been struggling through it alot.

WE have a package created in database which is having a procedure with (5 input variables and 1 out variable (which is a sys refcursor).
Now the rason why we have used sysrefcursors is because there are crores of records in the tables and we dont want to have any performance issues.

So example
:
Create or replace package emp_search

Procedure get_data(emp IN varchar2, deptno. IN number, sysrefcursor OUT)
-----
-----
end;

Now this refcursor will get those records very fast as compared to normal select statement.

But the issue is I have to use this out variable : SYS.REFCURSOR as a source to display a report in APEX.

So suppose I wont to built a classic report such as -

SELECT * FROM EMP_SEARCH.GETDATA...(I know this is wrong but I need something like this sql or some pl/sql block to get the data in the classic report fetching the data from that sys.refcursor)
How to do that
?????????//

This is veryy urgent , I have been struggling through this since long . Please provide some useful and fast solution.



and Connor said...

It would be worth asking this question on the Apex forum

https://community.oracle.com/community/database/developer-tools/application_express

But one method you could use is a pipelined function.

https://asktom.oracle.com/pls/apex/f?p=100:11:::NO:RP:P11_QUESTION_ID:9525241900346881302

which means you can base your report on a query like

select ...
from table(my_function);




Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Martin Rose, August 18, 2016 - 10:43 am UTC

> Now this refcursor will get those records very fast as compared to normal select statement.

There won't be any difference in speed between the two methods. Why do you think there might be?
Connor McDonald
August 18, 2016 - 11:09 pm UTC

My *guess* is this is reference to the *client* processing,

"select" = fetch all
ref cursor = fetch piecewise


Convert Ref cursor to XMLTYPE

Jonathan Taylor, August 19, 2016 - 5:23 pm UTC

You could convert the Ref cursor to an XMLTYPE, and then parse the XML in SQL.
This could be done by having a function returning SYS_REFCURSOR, wrapped in XMLTYPE in the query (as below).

You do have to be careful of very large result sets, and implicit date conversions to text.

If this must be an OUT parameter SYS_REFCURSOR, it would be trickier:-
1. Save PL/SQL SYS_REFCURSOR variable into an APEX hidden item, e.g.
:P1000_XML := XMLTYPE(v_SYS_REFCURSOR).getClobVal();
2. Refer to XMLTYPE(:P1000_XML) instead of TEST_RC() function in example below:-

DROP FUNCTION test_rc;
CREATE OR REPLACE FUNCTION TEST_RC RETURN SYS_REFCURSOR
IS
  v_RC SYS_REFCURSOR;
BEGIN
  OPEN v_RC FOR
       SELECT object_name,last_DDL_time
       FROM   ALL_OBJECTS
       WHERE OWNER='SYS'
       AND   ROWNUM <= 10;
  RETURN (v_RC);
END;
/

SELECT *
FROM   XMLTABLE ('/ROWSET/ROW'
                PASSING XMLTYPE(Test_RC())
                COLUMNS 
                  object_name  VARCHAR2(100) PATH '/ROW/OBJECT_NAME'
                , last_DDL_time VARCHAR2(100) PATH '/ROW/LAST_DDL_TIME'
                );

Connor McDonald
August 20, 2016 - 3:21 am UTC

Nice touch.

Does not work

Andrew Crompton, November 24, 2016 - 2:08 pm UTC

Tried above method in APEX 5
and got the following

report error:
ORA-14551: cannot perform a DML operation inside a query

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library