| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Procedure to return a result set
On 8 May 1997 20:50:08 GMT, "Bob Weber" <bweber_at_onramp.net> wrote:
>Ok! I give up!!
>It's very simple under MS SQL to write a stored procedure which returns a
>result set. Unfortunately I can't seem to figure out how to do the same
>thing under ORACLE!!!
>
>Could anyone help? let's say I want a procedure to
>SELECT PRICE FROM PRICE_TABLE WHERE VENDOR = :VEND
>how can i create a procedure to do this and how can i successfully execute
>ie. EXECUTE SP_PRICE( "ORACLE");
>and then get back
>PRICE
>----------
>1.00
>2.00
>3.45
>etc...
>
>Sorry to seem so dense, but I seem to be missing something very basic!
>Thanks,
>Bob Weber
>bweber_at_onramp.net
The following was done in 7.3 of the database. This capability started with 7.2 of the database. The syntax would be a little different for 7.2 (let me know if you need that one). You need to create one types package (i have one in my database where I store lots of common types like record defs and array types for applications). In it, you type a cursor reference type.
create or replace package types
as
type curs is ref cursor;
end types;
/
Then you create a procedure that takes that type as an in/out parameter and any other inputs you want. You could use the inputs to open the query as bind variables although in this example I am opening a different query based on the inputs.
create or replace procedure show_cursor( p_cursor in out types.curs,
p_input in varchar2 default NULL)
as
if ( upper( p_input ) = 'DEPT' ) then
open p_cursor for select * from dept; else
open p_cursor for select * from emp;
end if;
end;
/
Then, to get this to work in SQL*Plus, you declare a variable C to hold the cursor, call the stored procedure to get the cursor, and print it as such:
variable C refcursor
exec show_cursor( p_cursor => :C, p_input => 'dept' )
print C
exec show_cursor( p_cursor => :C )
print C
In Pro*C this might look something like:
REM ----------- PRO*C Getting the cursor ------------------REM
REM typedef char asciiz; REM EXEC SQL TYPE asciiz IS STRING(100); REM SQL_CURSOR my_cursor; REM asciiz ename[40]; REM int mgr; REM short mgr_i; REM int deptno; REM asciiz dname[50]; REM asciiz loc[50]; REM int i;
REM EXEC SQL EXECUTE BEGIN empCurType.open_cursor( :my_cursor, 'EMP' ); end;
REM END-EXEC;
REM
REM for( ;; )
REM {
REM EXEC SQL WHENEVER NOTFOUND DO BREAK;
REM EXEC SQL FETCH :my_cursor
REM INTO :ename, :mgr:mgr_i, :deptno, :dname, :loc;
REM
REM printf( "%s, %d, %d, %s, %s\n",
REM ename, mgr_i?-1:mgr, deptno, dname, loc );
REM }
REM EXEC SQL CLOSE :my_cursor;
http://govt.us.oracle.com/ -- downloadable utilities
![]() |
![]() |