Xref: alice comp.databases.oracle.server:24163
Path: alice!news-feed.fnsi.net!newsfeed.wli.net!su-news-hub1.bbnplanet.com!su-news-feed1.bbnplanet.com!news.bbnplanet.com!inet16.us.oracle.com!not-for-mail
From: tkyte@us.oracle.com (Thomas Kyte)
Newsgroups: comp.databases.oracle.server
Subject: Re: Returning a result set from a table using a stored procedure
Date: Tue, 07 Jul 1998 17:08:13 GMT
Organization: Oracle Government
Lines: 121
Message-ID: <35a45544.15441764@192.86.155.100>
References: <01bda8f6$921f7c00$9a6adec2@ajaytosh> <35A1022C.BC291B8A@contractor.net> <35a20d2f.0@newsread1.dircon.co.uk>
Reply-To: tkyte@us.oracle.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Newsreader: Forte Agent 1.5/32.451

A copy of this was sent to "Graeme Humphrey" <graeme.humphrey@prosoftres.co.uk>
(if that email address didn't require changing)
On Tue, 7 Jul 1998 13:01:34 +0100, you wrote:

>Any ideas how to do this in Visual C++ v1.52?
>
>Scott Cote wrote in message <35A1022C.BC291B8A@contractor.net>...
>>Look at using reference cursors (and binding the cursor to your host
>>programming lang.).
>>
>>SCott
>>Hive Software, Inc.
>>
>>Ajay Soni wrote:
>>
>>> Hi,
>>>
>>> We're trying to return a table by calling a stored procedure which has a
>>> select clause inside ( which looks like SELECT * FROM MYCUSTOMERS ).
>>>
>>>


if you use pro*c, it'll look like this.  There is an oci example in  oci11.c (on
NT found in \orant\ociXX\samples\msvc where XX is your database version)..



create or replace function sp_ListEmp return types.cursortype
as
    l_cursor    types.cursorType;
begin
    open l_cursor for select ename, empno from emp order by ename;
    return l_cursor;
end;
/


With 7.2 on up of the database you have cursor variables.  Cursor variables
are cursors opened by a pl/sql routine and fetched from by another application
or pl/sql routine (in 7.3 pl/sql routines can fetch from cursor variables as
well as open them). The cursor variables are opened with the privelegs of the
owner of the procedure and behave just like they were completely contained
within the pl/sql routine. It uses the inputs to decide what database it will
run a query on.

Here is an example:



create or replace package types
as
    type cursorType is ref cursor;
end;
/
 
create or replace function sp_ListEmp return types.cursortype
as
    l_cursor    types.cursorType;
begin
    open l_cursor for select ename, empno from emp order by ename;
 
    return l_cursor;
end;
/
 
 
REM SQL*Plus commands to use a cursor variable
 
variable c refcursor
exec :c := sp_ListEmp
print c



-----------------------------------------------------

and the Pro*c to use this would look like:


static void process()
{
EXEC SQL BEGIN DECLARE SECTION;
    SQL_CURSOR  my_cursor;
    VARCHAR     ename[40];
    int         empno;
EXEC SQL END DECLARE SECTION;
 
    EXEC SQL WHENEVER SQLERROR DO sqlerror_hard();
 
    EXEC SQL ALLOCATE :my_cursor;
 
    EXEC SQL EXECUTE BEGIN
        :my_cursor := sp_listEmp;
    END; END-EXEC;
 
    for( ;; )
    {
        EXEC SQL WHENEVER NOTFOUND DO break;
        EXEC SQL FETCH :my_cursor INTO :ename, empno;
 
        printf( "'%.*s', %d\n", ename.len, ename.arr, empno );
    }
    EXEC SQL CLOSE :my_cursor;
}


 
Thomas Kyte
tkyte@us.oracle.com
Oracle Government
Herndon VA
 
http://govt.us.oracle.com/    -- downloadable utilities
 
----------------------------------------------------------------------------
Opinions are mine and do not necessarily reflect those of Oracle Corporation
 
Anti-Anti Spam Msg: if you want an answer emailed to you, 
you have to make it easy to get email to you.  Any bounced
email will be treated the same way i treat SPAM-- I delete it.
