Oracle – Calling Oracle package procedures which return ref cursors in straight PL/SQL

ado.netcursororacleoracle10gstored-procedures

I've got an Oracle 10g database which is accessed from an ASP.NET application. Although I've used SQL Server heavily in many different aspects and Oracle for querying and reporting, this is my first time using Oracle as the OLTP database for an application.

The database-level procedures in the packages are typically of the form:

-- TYPE refcur IS REF CURSOR;

PROCEDURE get_some_stuff(o_cursor OUT refcur, p_param1 IN INTEGER, p_param2 IN INTEGER) IS
BEGIN
    OPEN o_cursor FOR
    SELECT whatever
    FROM whatever
END

I assume these are done this way for the benefit of the ADO.NET layer able to use the cursor from the output param and it is my understanding that this is the acceptable best practice for calling Oracle procs from .NET.

In SQL Server, for example, we don't have explicit ref cursors, if a proc returns a result set (or several result sets), that's accessible as an output result set in both ADO.NET and SSMS, and you can simply test the SPs by doing EXEC spname param1, param2.

The problem I'm having is that I don't know how to call these directly in SQL in Toad, for example, to be able to test changes at the PL/SQL level first before going to the app. I'm very used to being able to exercise and even re-mix stored procs and functions in SQL Server to be able to refactor the database interface layer without affecting the external interface to application-level code.

Best Answer

look at the link that OMG Ponies posted, but what you can do is

    var x refcursor;


declare

    PROCEDURE GET_SOME_STUFF(O_CURSOR OUT SYS_REFCURSOR, P_PARAM1 IN NUMBER, P_PARAM2 IN NUMBER) IS
    BEGIN
         OPEN O_CURSOR FOR
         SELECT LEVEL, p_param1 ,P_PARAM2  FROM DUAL CONNECT BY LEVEL < 3;
    END ;

BEGIN
GET_SOME_STUFF(:x , 5, 10); 
END;
/

PRINT X;

you pretty much just wrap it in a anonymous block ad it will run. I use SQL Developer (highly recommmend, free with plenty of support) or SQL plus so I cannot help with TOAD, but I would expect it to be the same. In SQL Developer (and in SQL Navigator if memory serves correct) you can simply right click the package/method you wish and it will create the script for you.
in toad and navigator I believe you may be able to get the ref cursor in a pretty grid while in developer you get it in text.

SQL Developer you can unit test as well