3

I have kind of a tricky Oracle problem. I am trying to select one set of data, we'll call items. For each item I want to call another procedure and return an Inventory Item. I have two operations I am not sure on how to perform.

  1. How do I retrieve a value from the nested procedure?

  2. How do I return those retrieved values in the form of SYS_REFCURSOR?

My attempt here was to put the results from spSelect_Inv_Search into a nested table called ITEMS_TABLE. This is not working.

Code below

PROCEDURE SPSELECT_ITEM (IO_CURSOR OUT SYS_REFCURSOR) 
AS   
  MY_CURSOR SYS_REFCURSOR;
  TYPE ITEM_TYPE IS TABLE OF ITEMS.ITEM_NO%TYPE;
  ITEM_TABLE ITEM_TYPE := ITEM_TYPE();

  CURSOR ITEMS_CURSOR IS
      SELECT ITEM_NO 
      FROM ITEMS;

  V_COUNTER INTEGER := 0;
BEGIN
  FOR ITEM_REC IN ITEM_CURSOR LOOP
    V_COUNTER := V_COUNTER + 1;
    ITEM_TABLE.EXTEND;
    ITEM_TABLE(V_COUNTER) := spSelect_Inv_Search(ITEM_REC.ITEM_NO, MY_CURSOR);
  END LOOP;
END SPSELECT_ITEMS;

Any help is appreciated, thanks.

  • Can you explain exactly what is not working? Are you getting an error? Is the procedure doing something you don't expect or not doing something you do expect? What does `spSelect_Inv_Search` return? What do you want `IO_CURSOR` to reference when the procedure completes? – Justin Cave May 18 '11 at 16:21
  • spselect_Inv_Search returns an SYS_REFCURSOR. I don't know how to retrieve anything from spSelect_Inv_Search. That is my first problem. IO_CURSOR should reference the nested table created from loop calling the spSelect_Inv_Search. That is my second problem. –  May 18 '11 at 16:31

2 Answers2

0

In to answer your question about how to call spSelect_Inv_Search, I'd need to know the signature of that subprogram. You've described it as a procedure but you're trying to call it as a function. Which is it? What return value and/or OUT-mode parameters does it have?

To return an open REF CURSOR from the above procedure, first the nested table type needs to be declared at the schema level (using a CREATE TYPE statement) instead of in the PL/SQL code. Then you can open the cursor like so, after populating the nested table.

OPEN io_cursor FOR SELECT * FROM TABLE(CAST(item_table AS item_type));

(And by the way, I would change the name of the type from ITEM_TYPE to ITEM_TABLE_TYPE, myself.)

Dave Costa
  • 47,262
  • 8
  • 56
  • 72
  • spSelect_Inv_Search has 18 inputs and one output of type SYS_REFCURSOR. It is a procedure, that is the only way I've seen to call it. –  May 18 '11 at 16:40
0

You seem to be wanting to merge an unknown number of SYS_REFCURSOR result sets into one big one. If you know the structure of the cursor returned from spSelect_Inv_Search you can do this with an intermediate pipelined function.

create package p as
    type tmp_rec_type is record (owner all_objects.owner%type,
        object_type all_objects.object_type%type,
        objects number);
    type tmp_rec_table is table of tmp_rec_type;

    procedure proc1(p_owner in varchar2, p_cursor out sys_refcursor);
    function func2 return tmp_rec_table pipelined;
    procedure proc3(p_cursor out sys_refcursor);
end;
/

The types can be defined here, they don't have to be at SQL level as you won't ever need to reference them outside the package.

create package body p as
    procedure proc1(p_owner in varchar2, p_cursor out sys_refcursor) as
    begin
        open p_cursor for select owner, object_type, count(*)
            from all_objects
            where owner = p_owner
            group by owner, object_type;
    end;

    function func2 return tmp_rec_table pipelined as
        cursor c1 is select distinct owner
            from all_tables where owner in ('SYS','SYSTEM');
        tmp_cursor sys_refcursor;
        tmp_rec tmp_rec_type;
    begin
        for r1 in c1 loop
            proc1(r1.owner, tmp_cursor);
            loop
                fetch tmp_cursor into tmp_rec;
                exit when tmp_cursor%notfound;
                pipe row(tmp_rec);
            end loop;
        end loop;
    end;

    procedure proc3(p_cursor out sys_refcursor) as
    begin
        open p_cursor for select * from table(func2);
    end;
end p;
/

Then to execute, which you can do outside the package despite the types used for the intermediate stage, you can do this to test in SQL*Plus or SQL Developer:

var rc refcursor;
exec p.proc3(:rc);
print rc;

For my database this gives:

OWNER                          OBJECT_TYPE         OBJECTS                
------------------------------ ------------------- ---------------------- 
SYSTEM                         VIEW                1                      
SYSTEM                         TABLE               5                      
SYS                            VIEW                1056                   
SYS                            CONSUMER GROUP      2                      
SYS                            PROCEDURE           11                     
SYS                            FUNCTION            56                     
SYS                            SEQUENCE            1                      
SYS                            OPERATOR            6                      
SYS                            EVALUATION CONTEXT  1                      
SYS                            TABLE               13                     
SYS                            WINDOW GROUP        1                      
SYS                            PACKAGE             162                    
SYS                            WINDOW              2                      
SYS                            TYPE                529                    
SYS                            JOB CLASS           1                      
SYS                            SCHEDULE            1     

This is obviously very contrived as you'd do this as a single query, but I'm assuming your inner procedure needs to do something more complicated.

Alex Poole
  • 183,384
  • 11
  • 179
  • 318
  • This seems to be exactly what I need. Unfortunately I do not know the exact structure that is returned in spSelect_Inv_Search's SYS_REFCURSOR. I will have to find that out to complete testing. –  May 18 '11 at 19:29
  • Okay, after getting the Inv_Search structure figured out, this is working. However, it is very slow. Is there a way to perform this function quicker? –  May 18 '11 at 20:36
  • Is the function really adding much overhead, or is it just repeated calls to the inner procedure that are collectively slow? I've often been surprised how what I *think* is a reasonably quick call based on a single execution is actually slow when called repeatedly. (Now I usually stress-test such things by calling them a thousand times in a loop to get a more accurate picture of the real execution time). – Alex Poole May 18 '11 at 21:10