簡體   English   中英

如何從多個游標從oracle存儲過程返回多行?

[英]How to return multiple rows from oracle stored procedure from multiple cursors?

我需要具有可以運行多個游標的存儲過程。

循環遍歷每個游標,然后對每一行進行一些操作。

這樣,我將從這些游標中獲得所需的結果。 這樣的多個游標的結果需要與其他一些行合並,然后過濾掉並最終從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;    

我的建議是將光標中的行插入到臨時表中。 然后將臨時表與您現有的表連接起來,以提及您所提到的過濾條件。 偽代碼:

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;  
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很簡單create two nested table ,do some manipulation on both the nested table

假設您有v_emp_nt1作為first nested table ,您對此做了一些操作。 您有另一個v_emp_nt2作為second nested table ,您對此做了一些操作。

現在你的cursor會像

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

通過這種方式,您可以實現所需的輸出。

**注意:**上面的代碼是針對一個嵌套表的,您需要創建另一個嵌套表才能使代碼完整

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM