Solved

retrieving data from a ref cur when calling from a stored procedure

Posted on 2014-09-29
6
260 Views
Last Modified: 2014-09-29
Hello,
Is this the correct way in retrieving information using a ref cur when calling from a stored procedure?
Thanks.

--Main Procedure..
  PROGCDXREF ref cursor;
  WS_PROGRAM_NM varchar2(20) := 'TNT';
  ws_program_cd varchar2(5);

--execute the procedure to retrieve the program code...
  SELECT_PROGRAM_CD_XREF(ws_program_nm, PROGCDXREF);

  WS_PROGRAM_CD := PROGCDXREF.PROGRAM_CD;

--End of Main Procedure...  
 
--Procedure - Select_Program_CD_Xref...
procedure SELECT_PROGRAM_CD_XREF(V_PROGRAM_NM varchar2,
                                CUR_SELECT_PROGRAM_CD_XREF OUT REF CURSOR)
IS
begin
  Open cur_select_Program_CD_Xref For
  SELECT
    Program_NM,
    Program_CD,
    deactivate_dt
  FROM Program_CD_Xref
  WHERE PROGRAM_NM = V_PROGRAM_NM;
 
End Select_Program_CD_Xref;
0
Comment
Question by:Machinegunner
  • 3
  • 3
6 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40350322
Did you try it?

No.  Once the ref cursor is returned, you need to FETCH the values.

You typically do this in a loop.

There is an example here.  It makes no difference if the cursor is created in the same code or created from a procedure call.  The loop and FETCH is the same.

http://docs.oracle.com/cd/E11882_01/server.112/e41084/expressions006.htm#SQLRF52079
0
 

Author Comment

by:Machinegunner
ID: 40350498
Thanks for the reply,
Yes, the above code is within another loop that a regular cursor selects the
program_nm into the variable, ws_program_nm.  
You would think that the ref cursor - PROGCDXREF will return the values from the
select procedure and then you can select individual columns within that ref cur.
Thanks.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40350510
A cursor isn't a true result set.  It is only a pointer to the 'query'.  Just opening it doesn't really do anything.  It isn't until you fetch the data is there any data available.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Machinegunner
ID: 40350545
Just looking how this code is just supposed to retrieve a program code, it would seem prudent to just write a function to do this, instead of using a ref cursor.  Something like this:

--Main Procedure..
  WS_PROGRAM_NM varchar2(20) := 'TNT';
  ws_program_cd varchar2(5);

--execute the procedure to retrieve the program code...
  Select GET_PROGRAM_CD(ws_program_nm) into ws_program_cd from dual;

--End of Main Procedure...  
 
function GET_PROGRAM_CD(V_PROGRAM_NM varchar2)
is varchar2
  v_program_cd varchar2(3);
begin

  SELECT
    Program_CD,
  FROM Program_CD_Xref
  where PROGRAM_NM = V_PROGRAM_NM;
 
End;
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40350554
If the result will ALWAYS be one and only one value, then a function with a single select would probably be the way to go.

The stub example you posted needs a little work though.

If the code can return more than one row and/or more than one value then something that allows a 'collection' to be returned is the way to go.  Returning a cursor is just one way.  Something that returns XML as a CLOB is another.  There are others.
0
 

Author Closing Comment

by:Machinegunner
ID: 40350562
Thanks for the information.
I am going to go ahead and just use this function psuedo-code as an example, as this will be used many times
within the main procedure(s) to retrieve a program code, so will be the best approach to this.
Thanks again!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now