6

I would like to select from a function that return a SYS_REFCURSOR Type value.

for example :

CREATE OR REPLACE FUNCTION my_funtion (
 my_param IN VARCHAR2)
 RETURN SYS_REFCURSOR
 IS
 l_return SYS_REFCURSOR;
 BEGIN
 OPEN l_return FOR
 SELECT last_name, first_name
 FROM employees
 WHERE id = my_param
 ORDER BY employee_id;
 RETURN l_return;
END my_funtion;

I would like to do something similar to this :

select * from my_function('id015');

or even :

select alias.last_name from my_function('id015') alias;
asked Feb 12, 2015 at 21:09
1

2 Answers 2

5

You could try using select * from table(xmlsequence( myfunc() )) . In this case you get xml in columns.

Or try this example to extract fields from your refcursor

select extractvalue(column_value,'/ROW/FIRST_NAME') first_name
 , extractvalue(column_value,'/ROW/LAST_NAME') last_name
from table(xmlsequence(f()));
FIRST_NAME | LAST_NAME
:--------- | :--------
Donald | Duck 
Mickey | Mouse 

dbfiddle here

PS. Note about xml overhead.

Jack Douglas
40.6k16 gold badges106 silver badges179 bronze badges
answered Oct 30, 2015 at 10:30
1
  • 1
    Does this solution closes the sys_refcursor internally? Commented Jan 15, 2019 at 15:26
0

This worked for me:

variable x refcursor 
set autoprint on 
exec :x := my_function('id015')
answered Oct 24, 2017 at 10:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.