Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test an Oracle Stored Procedure with RefCursor return type?

I'm looking for a good explanation on how to test an Oracle stored procedure in SQL Developer or Embarcardero Rapid XE2. Thank you.

like image 265
Ryan Fisch Avatar asked Jul 21 '11 19:07

Ryan Fisch


People also ask

How do I run a ref cursor in Oracle?

With a cursor variable, you simply pass the reference to that cursor. To declare a cursor variable, you use the REF CURSOR is the data type. PL/SQL has two forms of REF CURSOR typeS: strong typed and weak typed REF CURSOR . The following shows an example of a strong REF CURSOR .

Can ref cursor be used with procedure?

A ref cursor is a variable, defined as a cursor type, which will point to, or reference a cursor result. The advantage that a ref cursor has over a plain cursor is that is can be passed as a variable to a procedure or a function.


2 Answers

Something like

create or replace procedure my_proc( p_rc OUT SYS_REFCURSOR ) as begin   open p_rc    for select 1 col1          from dual; end; /  variable rc refcursor; exec my_proc( :rc ); print rc; 

will work in SQL*Plus or SQL Developer. I don't have any experience with Embarcardero Rapid XE2 so I have no idea whether it supports SQL*Plus commands like this.

like image 165
Justin Cave Avatar answered Sep 30 '22 14:09

Justin Cave


Something like this lets you test your procedure on almost any client:

DECLARE    v_cur SYS_REFCURSOR;   v_a   VARCHAR2(10);   v_b   VARCHAR2(10); BEGIN   your_proc(v_cur);    LOOP     FETCH v_cur INTO v_a, v_b;     EXIT WHEN v_cur%NOTFOUND;     dbms_output.put_line(v_a || ' ' || v_b);   END LOOP;   CLOSE v_cur; END; 

Basically, your test harness needs to support the definition of a SYS_REFCURSOR variable and the ability to call your procedure while passing in the variable you defined, then loop through the cursor result set. PL/SQL does all that, and anonymous blocks are easy to set up and maintain, fairly adaptable, and quite readable to anyone who works with PL/SQL.

Another, albeit similar way would be to build a named procedure that does the same thing, and assuming the client has a debugger (like SQL Developer, PL/SQL Developer, TOAD, etc.) you could then step through the execution.

like image 38
DCookie Avatar answered Sep 30 '22 13:09

DCookie