Result Set returned from a Stored Procedure 
Author Message
 Result Set returned from a Stored Procedure
Does anyone know a workaround to return a result set from a stored procedure
in Oracle ? Thanks.


Mon, 27 Aug 2001 03:00:00 GMT
 Result Set returned from a Stored Procedure

Quote:



>> Does anyone know a workaround to return a result set from a stored
procedure
>> in Oracle ? Thanks.

>Result sets in Oracle are called cursors. You can declare variable of
>type REF CURSOR, open it and return it.

>Mladen Gogala

Is this REF CURSOR variable useable when the stored procedure is called from
ODBC?
Corn


Tue, 28 Aug 2001 03:00:00 GMT
 Result Set returned from a Stored Procedure

(if that email address didn't require changing)

Quote:




>>> Does anyone know a workaround to return a result set from a stored
>procedure
>>> in Oracle ? Thanks.

>>Result sets in Oracle are called cursors. You can declare variable of
>>type REF CURSOR, open it and return it.

>>Mladen Gogala

>Is this REF CURSOR variable useable when the stored procedure is called from
>ODBC?
>Corn

In short, it'll look like this:

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;

Quote:
}


If you use ODBC here is a working example, but it requires the use of the
8.0.5.2.0 or later Oracle ODBC driver, and an 8.0.5 server.

'
' 1) Create a form with 1 Text control (Text1) and 1 List Control
(List1) and
'    1 Button (btnExecute).
' 2) The only code that you need is a Click method on your button.
Here is the Code.
'
'
Private Sub btnExecute_Click()
'PL/SQL Code
'===========
'
'CREATE OR REPLACE package reftest as
' cursor c1 is select ename from emp;
' type empCur is ref cursor return c1%ROWTYPE;
' Procedure GetEmpData(en in varchar2,EmpCursor in out empCur);
'END;
'
'
'CREATE OR REPLACE package body reftest as
'   Procedure GetEmpData
'(en in varchar2,EmpCursor in out empCur) is
'begin
' open EmpCursor for select ename from emp where ename LIKE en;
'end;
'end;
'
     Dim cn As New rdoConnection
     Dim qd As rdoQuery
     Dim rs As rdoResultset
     Dim cl As rdoColumn
     Static Number As Integer

     List1.Clear
     Number = 0
     cn.Connect = "uid=scott; pwd=tiger; DSN=MSLANGORL;"
     'enable the MS Cursor library
     cn.CursorDriver = rdUseOdbc
     'Make the connection
     cn.EstablishConnection rdNoDriverPrompt

     sSQL = "{call RefTest.GetEmpData(?,?)}"

     Set qd = cn.CreateQuery("", sSQL)

     qd.rdoParameters(0).Type = rdTypeVARCHAR
     qd(0).Direction = rdParamInputOutput
     qd(0).Value = Text1.Text
     qd.rdoParameters(1).Type = rdTypeVARCHAR

     'Dynamic or Keyset is meaningless here
     Set rs = qd.OpenResultset(rdOpenStatic)

     Do
        Debug.Print
        Debug.Print

        Do Until rs.EOF
            For Each cl In rs.rdoColumns
                 If IsNull(cl.Value) Then
                    List1.AddItem "(null)"
                    ' Debug.Print " "; cl.Name; "NULL"; Error trap for
null fields
                Else
                    List1.AddItem cl.Value
                    ' Debug.Print " "; cl.Name; " "; cl.Value;
                End If
            Next
            Debug.Print
            rs.MoveNext
        Loop
     Loop While rs.MoreResults
     cn.Close

End Sub

Thomas Kyte

Oracle Service Industries
Reston, VA   USA

--
http://govt.us.oracle.com/    -- downloadable utilities

----------------------------------------------------------------------------
Opinions are mine and do not necessarily reflect those of Oracle Corporation



Tue, 28 Aug 2001 03:00:00 GMT
 Result Set returned from a Stored Procedure
Hi!


Quote:
>>Result sets in Oracle are called cursors. You can declare variable of
>>type REF CURSOR, open it and return it.
>>Mladen Gogala
>Is this REF CURSOR variable useable when the stored procedure is called
from
>ODBC?
>Corn

If you using OCI - no problem
ODBC & DBE - have many problem - this depending on versions ODBC
Oracle Stored procedure return cursor  and client(ODBC,DBE) can't receiving.

Best regards
? a?
---
Origin ... e??? ??? ?T??? ??? ? ? ??...
Romanov Alexander

ICQ 6220754



Wed, 29 Aug 2001 03:00:00 GMT
 
 [ 5 post ] 

 Relevant Pages 

1. returning result sets from stored procedures

2. Returning result sets in oracle stored procedure (OLEDB)

3. return multiple result sets from a stored procedure

4. Can't update result set returned from stored procedure using RDO

5. Returning result set from function to stored procedure is very slooooooooow

6. best way to return a paged result set from a stored procedure

7. Returning a result set from a Stored Procedure

8. returning result sets from stored procedures

9. How not to return multiple result sets from a stored procedure

10. Stored Procedure/Multiple Return Results/Set Statement

11. return result set from stored procedure

12. return result set from stored procedure


 
Powered by phpBB® Forum Software