2010-05-06 161 views
9

這裏有兩個流水線功能包:管道函數調用另一個管道函數

create or replace type tq84_line as table of varchar2(25); 
/

create or replace package tq84_pipelined as 

    function more_rows return tq84_line pipelined; 
    function go  return tq84_line pipelined; 

end tq84_pipelined; 
/

螞蟻相應的包體:

create or replace package body tq84_pipelined as 

    function more_rows return tq84_line pipelined is 
    begin 

     pipe row('ist'); 
     pipe row('Eugen,'); 

     return; 

    end more_rows; 

    function go return tq84_line pipelined is 
    begin 

     pipe row('Mein'); 
     pipe row('Name'); 

     /* start */ 
     for next in (
      select column_value line from table(more_rows) 
     ) 
     loop 
      pipe row(next.line); 
     end loop; 
     /* end */ 

     pipe row('ich'); 
     pipe row('weiss'); 
     pipe row('von'); 
     pipe row('nichts.'); 

    end go; 

end tq84_pipelined; 
/

重要的是,去排序電話more_rows/* start *//* end */

我可以使用包如下:

select * from table(tq84_pipelined.go); 

這是所有罰款和花花公子,但我希望我能與more_rows一個簡單的調用替換/* start *//* end */之間的界線。

但是,這顯然是不可能的,因爲它生成了一個PLS-00221:'MORE_ROWS'不是一個過程,或者是未定義的

所以,我的問題是:真的沒有辦法捷徑循環?

編輯

顯然,從到目前爲止的答案,我的問題是不明確的。

包裝,如上所述的作品。

但我很困擾標記/* start *//* end */之間的6(即:SIX)行。我想用一條線代替它們。但我沒有找到任何方式做到這一點。

回答

7

流水線功能的要點是提供TABLE()函數。我不認爲有什麼辦法可以避免它。不幸的是,我們必須將其輸出分配給PL/SQL變量。我們無法分配管道函數的嵌套表像這樣nt := more_rows;由於

PLS-00653: aggregate/table functions are not allowed in PL/SQL scope 

所以SELECT ... FROM TABLE()它必須是。

我有一個稍微不同的解決方案供您考慮。我不知道它是否解決了你的潛在問題。

create or replace package body tq84_pipelined as 

    function more_rows return tq84_line pipelined is 
    begin 

     pipe row('ist'); 
     pipe row('Eugen,'); 

     return; 

    end more_rows; 

    function go return tq84_line pipelined is 
     nt1 tq84_line; 
     nt2 tq84_line; 
     nt3 tq84_line; 
     nt0 tq84_line; 
    begin 

     nt1 := tq84_line('Mein','Name'); 

     select * 
     bulk collect into nt2 
     from table(more_rows); 

     nt3 := tq84_line('ich','weiss','von','nichts.'); 

     nt0 := nt1 multiset union nt2 multiset union nt3; 

     for i in nt0.first..nt0.last 
     loop 
      pipe row(nt0(i)); 
     end loop; 

     return; 

    end go; 

end tq84_pipelined; 
/

正如我敢肯定,你知道(但對於其他求職者的利益)的MULTISET UNION語法glomming集合在一起,在Oracle 10g中引入的。

這個版本的GO()產生相同的輸出作爲您最初的實現:

SQL> select * from table(tq84_pipelined.go) 
    2/

COLUMN_VALUE 
------------------------- 
Mein 
Name 
ist 
Eugen, 
ich 
weiss 
von 
nichts. 

8 rows selected. 

SQL> 
+0

感謝您的回覆。但它並不能幫助我的'問題',因爲在這個構造中,我不僅有'for ...循環......結束循環'結構,但也是一個額外的'批量收集'。我的意思是,這不是一個問題,因爲我可以忍受它,但是我認爲它在視覺上更具吸引力,只需一行簡單的調用即可獲得更多的行,而無需進一步的循環,而不需要更多的循環。 – 2010-05-07 02:36:51

0

嘗試 select column_value line from table(tq84_line.more_rows) 即在查詢中包括包名稱。

+0

'選擇表(more_rows)COLUMN_VALUE線'工作正常。 所需的替換不。 – 2010-05-06 08:18:24