Home » SQL & PL/SQL » SQL & PL/SQL » Use REF CURSOR in SQL query (Oracle 11G)
Use REF CURSOR in SQL query [message #593735] Tue, 20 August 2013 08:58 Go to next message
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 #593739 is a reply to message #593735] Tue, 20 August 2013 09:28 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

What you want is here:

http://www.orafaq.com/node/980

Regards
Manu
Re: Use REF CURSOR in SQL query [message #593740 is a reply to message #593735] Tue, 20 August 2013 09:35 Go to previous messageGo to next message
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 #593743 is a reply to message #593740] Tue, 20 August 2013 10:00 Go to previous messageGo to next message
sinpeak
Messages: 59
Registered: January 2011
Location: india
Member
Thanks.

One question - you mean we CAN NOT use REF CURSOR in a SELECT statement ?
Re: Use REF CURSOR in SQL query [message #593744 is a reply to message #593743] Tue, 20 August 2013 10:03 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Yes we can use with the way you shown already.

Except that, I don't think so.
Re: Use REF CURSOR in SQL query [message #593745 is a reply to message #593743] Tue, 20 August 2013 10:07 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
sinpeak wrote on Tue, 20 August 2013 08:00
Thanks.

One question - you mean we CAN NOT use REF CURSOR in a SELECT statement ?


what problem are you trying to solve?
Re: Use REF CURSOR in SQL query [message #593746 is a reply to message #593743] Tue, 20 August 2013 10:43 Go to previous message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
sinpeak wrote on Tue, 20 August 2013 17:00
Thanks.

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
Previous Topic: Doubt in SELECT query
Next Topic: Parametric Cursor Example
Goto Forum:
  


Current Time: Mon Aug 18 06:01:29 CDT 2025