How do I retrieve values from a nested Oracle procedure?

喜你入骨 提交于 2019-12-23 16:03:34

问题


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.


回答1:


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.




回答2:


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.)



来源:https://stackoverflow.com/questions/6047548/how-do-i-retrieve-values-from-a-nested-oracle-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!