?
Solved

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

Posted on 2014-09-29
6
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 77

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 77

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

801 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