Search code examples
plsqloracle11goracle-sqldeveloper

Display rows in refcursor without PRINT?


I have a stored procedure that looks like this:

CREATE OR REPLACE PROCEDURE GET_USERS(cursor_ OUT SYS_REFCURSOR)
AS

BEGIN

OPEN cursor_ FOR
      SELECT * FROM SYS.dba_users; 
END;

I test it like this:

var mycursor refcursor;
exec GET_USERS ( :mycursor );
print mycursor;

The problem is that PRINT displays it in Script Output tab instead of Query Result (with grid). It's easier for me to read from the grid.

Question 1: How can I test the stored procedure so that the result is displayed in Query Result with the grid?

Question 2: What's the difference between F5 and the green arrow button when running a regular SELECT statement? When I highlight SELECT * FROM SYS.dba_users; and press F5 I get the results in Script Output. If I press the green arrow button the results are displayed in Query Result.


Solution

  • How can I test the stored procedure so that the result is displayed in Query Result with the grid

    You don't...unless you write your procedure as a FUNCTION and you use it in a SELECT Query.

    Like so:

    create or replace function ref_expensive_emps return sys_refcursor
    as
     c sys_refcursor;
    begin
     open c for select * from employees order by salary desc fetch first 10 rows only;
     return c;
    end  ref_expensive_emps;
    /
    
    select ref_expensive_emps() from dual;
    

    enter image description here

    Or execute that SELECT FROM DUAL as a script...

    REF_EXPENSIVE_EMPS()
    --------------------
    CURSOR STATEMENT : 1
    
    CURSOR STATEMENT : 1
    
    EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DATE           JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID COLUMN1                                                                                                                         
    ----------- -------------------- ------------------------- ------------------------- -------------------- ------------------- ---------- ---------- -------------- ---------- ------------- --------------------------------------------------------------------------------------------------------------------------------
            100 Suppo0rt             King                      Suppo0rt                  515.123.4567         21-AUG-18  07.09.58 AD_PRES      49243.75                                      90                                                                                                                                 
            101 Neena                Kochhar                   NKOCHHAR                  515.123.5368         21-SEP-89  00.00.00 AD_VP        34888.29                       100            90                                                                                                                                 
            102 Lex                  De Haan                   LDEHAAN                   515.123.4569         13-JAN-93  00.00.00 AD_VP        34888.29                       100            90                                                                                                                                 
            145 John                 Russell                   JRUSSEL                   011.44.1344.429268   01-OCT-96  00.00.00 SA_MAN       28735.94             .4        100            80                                                                                                                                 
            146 Karen                Partners                  KPARTNER                  011.44.1344.467268   05-JAN-97  00.00.00 SA_MAN       27710.55             .3        100            80                                                                                                                                 
            201 Michael              Hartstein                 MHARTSTE                  515.123.5555         17-FEB-96  00.00.00 MK_MAN       26685.16                       100            20                                                                                                                                 
            108 Nancy                Greenberg                 NGREENBE                  515.124.4569         17-AUG-94  00.00.00 FI_MGR       24634.38                       101           100                                                                                                                                 
            205 Shelley              Higgins                   SHIGGINS                  515.123.8080         07-JUN-94  00.00.00 AC_MGR       24634.38                       101           110                                                                                                                                 
            147 Alberto              Errazuriz                 AERRAZUR                  011.44.1344.429278   10-MAR-97  00.00.00 SA_MAN       24634.38             .3        100            80                                                                                                                                 
            168 Lisa                 Ozer                      LOZER                     011.44.1343.929268   11-MAR-97  00.00.00 SA_REP       23608.99            .25        148            80                                                                                                                                 
    
    10 rows selected. 
    

    Question 2: What's the difference between F5 and the green arrow button when running a regular SELECT statement?

    Regular select statement: We run the query, do a single fetch, leave the dataset open, and print the results in the grid. As you scroll through the grid, we do more fetches as necessary. If you exhaust the resultset or close the grid, then the work on the db is finished.

    F5/Execute as script: we run whatever is in the editor, or is highlighted, through our script engine (mimics SQL*Plus), and it gets printed in the script output panel. All rows are fetched.

    If you execute your SP through the Code Editor, you can see your Refcursors in grids, but not the same grid as the SQL Worksheet.

    enter image description here

    Oh and with a refcursor, you only get to open/read it once...