Use REF CURSOR in SQL query [message #593735] |
Tue, 20 August 2013 08:58  |
 |
sinpeak
Messages: 59 Registered: January 2011 Location: india
|
Member |
|
|
Hi,
I have a function :
FUNCTION WP19DUMMY(vname IN VARCHAR2) RETURN SYS_REFCURSOR
IS
tmpcur SYS_REFCURSOR ;
BEGIN
OPEN tmpcur FOR SELECT IVALUE FROM GENPARAMS WHERE NAME = vname;
RETURN tmpcur;
END WP19DUMMY;
I want to use the returned REF CURSOR in a SQL query.
So I ran the following @ SQL prompt :
SELECT WP19DUMMY('TD') FROM DUAL;
and I am gettng the following result :
WP19DUMMY('TD
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
IVALUE
-------------
0
BUT I do not want such a result. I want a result that will only return the IVALUE.
Any idea how can we achieve this ?
Thanks.
[Updated on: Tue, 20 August 2013 08:58] Report message to a moderator
|
|
|
|
Re: Use REF CURSOR in SQL query [message #593740 is a reply to message #593735] |
Tue, 20 August 2013 09:35   |
 |
Michel Cadot
Messages: 68765 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote:BUT I do not want such a result. I want a result that will only return the IVALUE.
In this case execute the query directly.
REF CURSOR is not made to be used by SQL*Plus but by YOUR application, the one you write.
You do not use a function returning a REF CURSOR with a SELECT statement.
If you want to use it in SQL*Plus, the proper way is to use a RECURSOR variable:
SQL> create or replace function f (vname in varchar2) return sys_refcursor
2 is
3 tmpcur sys_refcursor;
4 begin
5 open tmpcur for select * from emp where ename=vname;
6 return tmpcur;
7 end;
8 /
Function created.
SQL> var c refcursor
SQL> exec :c := f('KING')
PL/SQL procedure successfully completed.
SQL> print c
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7839 KING PRESIDENT 17/11/1981 00:00:00 5000 10
1 row selected.
Regards
Michel
[Updated on: Tue, 20 August 2013 09:37] Report message to a moderator
|
|
|
|
|
|
Re: Use REF CURSOR in SQL query [message #593746 is a reply to message #593743] |
Tue, 20 August 2013 10:43  |
 |
Michel Cadot
Messages: 68765 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
sinpeak wrote on Tue, 20 August 2013 17:00Thanks.
One question - you mean we CAN NOT use REF CURSOR in a SELECT statement ?
No, I meant this is not the correct way to use it, above all if you use it in SQL*Plus.
Regards
Michel
|
|
|