如何从多个游标从oracle存储过程返回多行?

时间:2012-05-26 06:47:49

标签: sql oracle stored-procedures cursor

我需要有可以运行多个游标的存储过程。

遍历每个游标,然后对每一行进行一些操作。

这样我将从这些游标获得所需的结果。这样的多个游标的结果然后需要与其他一些行结合,然后过滤掉并最终从proc返回这些行。

请注意,每个cusror和其他查询都有相同的列。

我不知道如何在oracle中这样做。

请帮帮我。

        create or replace PROCEDURE test_proc
    (
      -- some inputs 
      hc_cursor OUT SYS_REFCURSOR
    ) 

    IS 

    cursor cursor_one is 
        SELECT * FROM table_one ; 

    BEGIN    

     FOR current_row in cursor_one
      loop 

          -- do some modification on each row and return each modified row

      end loop; 

    cursor cursor_two is 
        SELECT * FROM table_one ; 

    BEGIN    

     FOR current_row in cursor_two
      loop 

          -- do some modification on each row and return each modified row
          -- append to result from first cursor 

      end loop; 


    -- union results from both these cusrors with some another query 
    -- now filter these records on some criterais 
    -- return finally

    END;    

3 个答案:

答案 0 :(得分:3)

我的建议是将光标中的行插入临时表。然后将临时表与现有表一起加入您提到的过滤条件。伪码:

create or replace function my_func
return sysrefcursor
is
    cursor cursor_one is 
        SELECT * FROM table_one ; 

    cursor cursor_two is 
        SELECT * FROM table_one ; 
    BEGIN    

     FOR current_row in cursor_one
      loop 

          -- do some modification on each row and insert into temporary table

      end loop; 



     FOR current_row in cursor_two
      loop 

          -- do some modification on each row and insert into temporary table

      end loop; 


    -- results from cursor 1 and 2 exist in temporary table

    open out_cursor for
     select t.* from
      my_temp_table t
      join
      my_other_table tt
      on (t.col1 = tt.col1) -- or whatever columns are appropriate
      where t.col2 = 'some criteria' -- or whatever filter criteria you like.

    return out_cursor;

    END;  

答案 1 :(得分:0)

create  type emp_obj AS object 
(
 empno    NUMBER (4)        
,ename  VARCHAR2(10)
,sal      number(7,2)
,job      varchar2(9)
);

CREATE TYPE EMP_NT AS TABLE OF emp_OBJ;


create or replace package test_pkg
IS
TYPE abc_cur is REF CURSOR;

procedure test_proc
(
p_rec IN OUT abc_cur
);

END test_pkg;
/

create or replace package body test_pkg
IS 
procedure test_proc
(
p_rec IN OUT abc_cur
)
IS
v_emp_nt emp_nt;
BEGIN

SELECT emp_obj(empno,ename,sal,job) BULK COLLECT INTO v_emp_nt FROM EMP;

FOR i in v_emp_nt.first..v_emp_nt.last 
LOOP

IF v_emp_nt(i).job='CLERK' THEN 

    v_emp_nt(i).sal := v_emp_nt(i).sal +200;

ELSIF v_emp_nt(i).job='MANAGER' THEN

    v_emp_nt(i).sal := v_emp_nt(i).sal +800;
END IF;

END LOOP;

open p_rec for select * from table(v_emp_nt); 

END test_proc;

END test_pkg;
/

正如您所看到的那样,我所做的是在nested table您的光标正在做什么)中获得所需的结果,并根据结果记录进行一些操作,以及更新嵌套表。

最后,我将从此updated nested table创建一个游标,并在打开后返回光标。 comparsion of result before and after

现在你的问题:How can you return append cursor

这很简单create two nested table ,do some manipulation on both the nested table

假设您v_emp_nt1first nested table,您可以对其进行一些操作。  你有另一个v_emp_nt2作为second nested table,你会对此进行一些操作。

现在您的cursor就像

 open p_rec FOR (select * from v_emp_nt1 union select * from v_empnt2);

通过这种方式,您可以实现所需的输出。

**注意:**以上代码适用于一个嵌套表,您需要为代码创建另一个嵌套表才能完成

答案 2 :(得分:0)

create
package my_pkg as

   type my_rec is record
   (
     <list your fields here>
   );

   type my_rec_tab is table of my_rec;

   function get_my_rows
     return my_rec_tab pipelined;

end my_pkg;

create
package body my_pkg as

   function get_my_rows
     return my_rec_tab pipelined
   as
   begin

      for c_cur in (select * from table_one)
      loop

         -- do some modification on the current row and return the modified row

         pipe row (c_cur);

      end loop;

      for c_cur in (select * from table_one)
      loop

         -- do some modification on the current row and return the modified row

         pipe row (c_cur);

      end loop;

      return;

   end get_my_rows;

end my_pkg;

select * from table(my_pkg.get_my_rows);