0

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

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

这样,我将从这些游标中获得所需的结果。这样多个游标的结果需要与其他一些行联合,然后过滤掉并最终从 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;    
4

3 回答 3

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;  
于 2012-05-26T16:48:58.177 回答
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并在打开后返回光标。 前后结果对比

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

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

假设你有v_emp_nt1as first nested table,你对它做一些操作。你有另一个v_emp_nt2,你对它second nested table做一些操作。

现在你cursor会像

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

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

**注意:**以上代码是针对一个嵌套表的,您需要创建另一个嵌套表才能使您的代码完整

于 2012-05-26T11:08:59.557 回答
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);
于 2012-05-28T08:05:47.770 回答