如何从嵌套 Oracle 过程中检索值?

发布于 2024-11-08 02:12:13 字数 790 浏览 1 评论 0原文

我遇到了一个棘手的 Oracle 问题。我正在尝试选择一组数据,我们将其称为项目。对于每个项目,我想调用另一个过程并返回一个库存项目。我有两项手术,我不确定如何执行。

  1. 如何从嵌套过程中检索值?

  2. 如何以 SYS_REFCURSOR 的形式返回这些检索到的值?

我在这里的尝试是将 spSelect_Inv_Search 的结果放入名为 ITEMS_TABLE 的嵌套表中。这是行不通的。

下面的代码

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;

非常感谢任何帮助,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白龙吟 2024-11-15 02:12:13

您似乎想要将未知数量的 SYS_REFCURSOR 结果集合并为一个大结果集。如果您知道从 spSelect_Inv_Search 返回的游标的结构,您可以使用中间管道函数来完成此操作。

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;
/

类型可以在此处定义,它们不必在 SQL 级别,因为您不需要在包外部引用它们。

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;
/

然后执行,尽管中间阶段使用了类型,但您可以在包外部执行此操作,您可以在 SQL*Plus 或 SQL Developer 中执行此操作以进行测试:

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

对于我的数据库,这给出:

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     

这显然是非常人为的,就像您所做的那样这是一个单一的查询,但我假设您的内部过程需要做一些更复杂的事情。

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.

柏林苍穹下 2024-11-15 02:12:13

为了回答有关如何调用 spSelect_Inv_Search 的问题,我需要知道该子程序的签名。您已将其描述为过程,但试图将其作为函数调用。是哪一个?它有什么返回值和/或 OUT 模式参数?

要从上述过程返回打开的 REF CURSOR,首先需要在架构级别(使用 CREATE TYPE 语句)而不是在 PL/SQL 代码中声明嵌套表类型。然后,在填充嵌套表后,您可以像这样打开游标。

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

(顺便说一下,我自己将类型的名称从 ITEM_TYPE 更改为 ITEM_TABLE_TYPE。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文