Skip to Main Content
  • Questions
  • How to Return Result Sets from Oracle Stored Procedures

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Balaji.

Asked: June 21, 2002 - 8:24 am UTC

Last updated: July 06, 2005 - 1:03 pm UTC

Version: 8.1.7

Viewed 1000+ times

You Asked

Thank you for accepting my question.

We are in the process of converting DB2 Stored Procedures to Oracle. In DB2, along with the Procedure specification, clause "RESULT SETS 1" is defined. The result sets are returned primarily through a "Open Cursor" Statement. Apart from this the procedure also has Return Statements (and these return statements also contain expression). These DB2 stored procedures are invoked from C++ programs and the result sets are used in the C++ programs for manipulation.


My Problem: How to handle this in Oracle Stored Procedures?
Issue 1 - Return Statement Cannot have expression in procedures (though in functions this is allowed).
Issue 2 - Result Set has to be returned from same function along with return statement with expression.(More than One Value cannot be returned in function).



and Tom said...

</code> http://asktom.oracle.com/~tkyte/ResultSets/index.html <code>



You will use a procedure or function and the procedure will have IN OUT variables of ref cursor types and the function can either return a ref cursor OR have parameters of IN OUT types that are ref cursors.

Examples in the link above.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Result Sets from Oracle Stored Procedures.

Balaji T, June 22, 2002 - 12:20 am UTC

Thank you for the clarification.
The example given narrates how to return result sets using functions.
But the stored procedures we are working on have result sets as well as a return statement with expression. Functions cannot anyway return more than one value (Result Set + Return Value).
Can we have a method to handle this in Stored Procedures (instead of functions)?


Tom Kyte
June 22, 2002 - 9:31 am UTC

create or replace function f( x in number,
result1 in OUT types.rc,
result2 in OUT types.rc ) return number


there is a function that takes as INPUT a number X, returns two result sets result1 and result2 and finally returns a number as a value.

Functions, like procedures, accept IN, IN OUT, or OUT parameters. A function is nothing more then a procedure that returns a values.

A stored procedure would look like:

create or replace procedure p( result in out types.rc )


and when you called it, instead of executing:

begin :x := f(); end;

you would:

begin p(:x); end;


is all -- they both can do this as handily as the next. Also, read the PLSQL documentation, it has alot to say about ref cursors.

Returning Result Sets through Oracle

Balaji T, June 24, 2002 - 2:18 am UTC

Yes. The suggestion was very very useful and we are able to solve the problems now.

more on returing results from Stored Procedures

Phillip Miller, July 06, 2005 - 12:58 pm UTC

The information in this article is excellent. Thank you. I'm a bit a of an Oracle newbie, and wanted to ask for an example of a Stored Procedure that I can use as the source of data for a report developed in Crystal Reports. I'd like to be able to pass the stored procedure 2 parameters (date and facility) and have it return the result set (date, facility, quantiy, calculations, etc.)

Thanks in advance

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library