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

前端 未结 6 810
庸人自扰
庸人自扰 2020-12-02 12:13

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

相关标签:
6条回答
  • 2020-12-02 12:49

    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.

    0 讨论(0)
  • 2020-12-02 12:49
    create or replace procedure my_proc(  v_number IN number,p_rc OUT SYS_REFCURSOR )
    as
    begin
    open p_rc
    for select 1 col1
         from dual;
     end;
     /
    

    and then write a function lie this which calls your stored procedure

     create or replace function my_proc_test(v_number IN NUMBER) RETURN sys_refcursor
     as
     p_rc sys_refcursor;
     begin
     my_proc(v_number,p_rc);
     return p_rc;
     end
     /
    

    then you can run this SQL query in the SQLDeveloper editor.

     SELECT my_proc_test(3) FROM DUAL;
    

    you will see the result in the console right click on it and cilck on single record view and edit the result you can see the all the records that were returned by the ref cursor.

    0 讨论(0)
  • 2020-12-02 12:50

    In Toad 10.1.1.8 I use:

    variable salida refcursor
    exec MY_PKG.MY_PRC(1, 2, 3, :salida)  -- 1, 2, 3 are params
    print salida
    

    Then, Execute as Script.

    0 讨论(0)
  • 2020-12-02 12:57

    I think this link will be enough for you. I found it when I was searching for the way to execute oracle procedures.

    The link to the page

    Short Description:

    --cursor variable declaration 
    variable Out_Ref_Cursor refcursor;
    --execute procedure 
    execute get_employees_name(IN_Variable,:Out_Ref_Cursor);
    --display result referenced by ref cursor.
    print Out_Ref_Cursor;
    
    0 讨论(0)
  • 2020-12-02 12:59

    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.

    0 讨论(0)
  • 2020-12-02 13:05

    In SQL Developer you can right-click on the package body then select RUN. The 'Run PL/SQL' window will let you edit the PL/SQL Block. Clicking OK will give you a window pane titled 'Output Variables - Log' with an output variables tab. You can select your output variables on the left and the result is shown on the right side. Very handy and fast.

    I've used Rapid with T-SQL and I think there was something similiar to this.

    Writing your own delcare-begin-end script where you loop through the cursor, as with DCookie's example, is always a good exercise to do every now and then. It will work with anything and you will know that your code works.

    0 讨论(0)
提交回复
热议问题