2012-05-26 45 views
0

我需要存儲過程,我可以運行多個遊標。如何從多個遊標中的Oracle存儲過程返回多行?

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

這樣我就會從這些遊標中得到想要的結果。這樣多個遊標的結果需要與其他一些行結合,然後過濾出來並最終從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;  
+0

使用遊標結果提取到嵌套表,然後更新與操縱的結果。經過修改嵌套表,使用嵌套表...'SELECT * FROM表打開遊標(nested_table )'...用這種方式你可以返回多個結果光標 –

回答

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; 
+0

:這是更好的選項,我的:) +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); 

用這種方式,你可以達到你想要的輸出。

**注:**上面的代碼是一個嵌套表,您需要創建另一個嵌套表爲您的代碼,以便獲得完整

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); 
相關問題