【问题标题】:How to return dynamic cursor from oracle stored procedure如何从 oracle 存储过程返回动态游标
【发布时间】:2013-01-08 19:07:54
【问题描述】:

我有 2 个表,其中 ID 字段很常见。

我正在获取游标中第一个表的所有记录。 然后我想做的是根据游标中的每个ID,我想从第二个表中获取值然后返回。

我该怎么做... 请帮忙!!!

【问题讨论】:

    标签: oracle11g


    【解决方案1】:

    家庭作业?

    这是基本的 SQL。通常你会加入这两个表。

    begin
      for r_row in (select b.*
                      from tab1 a 
                           inner join tab2 b
                                   on b.id = a.id)
      loop
        null; -- do whatever
      end loop;
    end;
    /
    

    如果您有一个现有的光标并且无法更改它

    例如,your_cursor 只是返回一个 ID 列。

    begin
      open your_cursor;
      loop
        fetch your_cursor into v_id;
        exit when your_cursor%notfound;
        for r_row in (select * from tab2 b where b.id = v_id)
        loop
          null; -- do whatever here.
        end loop;
      end loop;
    end;
    /
    

    编辑: 根据 cmets:

    一些样本数据:

    SQL> create table table1 (id number primary key, name varchar2(20));
    
    Table created.
    
    SQL> create table table2 (id number, col1 varchar2(20), col2 varchar2(20));
    
    Table created.
    
    SQL> insert into table1 values (1, 'test');
    
    1 row created.
    
    SQL> insert into table1 values (2, 'foo');
    
    1 row created.
    
    SQL>
    SQL> insert into table2 values (1, 'John', 'Smith');
    
    1 row created.
    
    SQL> insert into table2 values (1, 'Peter', 'Jones');
    
    1 row created.
    
    SQL> insert into table2 values (1, 'Jane', 'Doe');
    
    1 row created.
    
    SQL> insert into table2 values (2, 'Nina', 'Austin');
    
    1 row created.
    
    SQL> insert into table2 values (2, 'Naman', 'Goyal');
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    

    创建一个类型来保存返回结构。请注意数据类型需要与表 table1table2 的数据类型匹配(%type 不起作用,因此请确保它们匹配)

    SQL> create type my_obj as object (
      2    id number,
      3    name varchar2(20),
      4    col1 varchar2(20),
      5    col2 varchar2(20)
      6  );
      7  /
    
    Type created.
    
    SQL> create type my_tab as table of my_obj;
      2  /
    
    Type created.
    

    现在创建你的函数(如果在你的真实代码中,你可以把它放在一个包中)。

    SQL> create function function1
      2    return my_tab pipelined
      3  is
      4  begin
      5    for r_row in (select t1.id, t1.name, t2.col1, t2.col2
      6                    from table1 t1
      7                         inner join table2 t2
      8                                 on t1.id = t2.id)
      9    loop
     10      pipe row(my_obj(r_row.id, r_row.name, r_row.col1, r_row.col2));
     11    end loop;
     12  end;
     13  /
    
    Function created.
    
    SQL>
    SQL> select *
      2    from table(function1);
    
            ID NAME                 COL1                 COL2
    ---------- -------------------- -------------------- --------------------
             1 test                 John                 Smith
             1 test                 Peter                Jones
             1 test                 Jane                 Doe
             2 foo                  Nina                 Austin
             2 foo                  Naman                Goyal
    

    如果需要,您可以将输入传递给该函数,例如 table(function1('a', 'b')); 等。

    【讨论】:

    • 感谢您的回复。您提供的代码 - for r_row in (select * from tab2 b where b.id = v_id) loop null; ——在这里做任何事。结束循环;在这段代码中,我将从 tab2 中获取值,但我想将 tab2 的这些值保留为少数 ID,并从存储过程中返回所有这些值。我该怎么做..
    • @NamanGoyal 你将如何使用它们?我们可以很容易地将它们作为数组返回。那会有帮助吗?或者你想要输出作为结果集(例如你想要select id from your_func()return_array = your_func()风格)
    • 下面是我正在使用的代码 - PROCEDURE "PROCEDURE1" IS id NUMBER;名称 VARCHAR2(200 字节); val1 VARCHAR2(200 字节); val2 VARCHAR2(200 字节); CURSOR cursor_ids IS SELECT ID, name FROM table1 BEGIN LOOP EXIT WHEN cursor_ids%NOTFOUND; FETCH cursor_ids INTO p_id, p_name;从表 2 结束循环中选择 col1、col2 到 val1、val2;结束“程序1”;假设 table1 中有 5 条记录。并且 table2 中的每个 id 都有多个条目。我想从 table2 中获取 table1 中每个 id 的最新值。然后从存储过程中返回所有数据。
    • @NamanGoyal “所有数据”只是 table2 中的 (col1, col2) 列表,还是 (p_id, p_name, col1, col2) 的数组?再一次,你想把它作为一个数组,还是按照我之前的评论作为一个结果集?
    • 我想将数据作为 (p_id, p_name, col1, col2) 的结果集
    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    相关资源
    最近更新 更多