【问题标题】:Pipelined function calling another pipelined function流水线函数调用另一个流水线函数
【发布时间】:2010-05-07 09:50:05
【问题描述】:

这是一个包含两个流水线函数的包:

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

ant对应的包体:

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

重要的是 go 某种调用 more_rows 与 for next in ... 在 /* start */ 和 /* end */ 之间

我可以按如下方式使用包:

select * from table(tq84_pipelined.go);

这一切都很好,很花哨,但我希望我可以用简单的more_rows 调用替换/* start */ 和/* end */ 之间的行。

但是,这显然是不可能的,因为它会产生 PLS-00221: 'MORE_ROWS' is not an procedure or is undefined。

那么,我的问题是:真的没有办法缩短循环吗?

编辑

显然,从目前的答案来看,我的问题并不清楚。

如前所述,该软件包有效。

但我对标记 /* start */ 和 /* end */ 之间的 6 行(即:6 行)感到困扰。我想用一行替换这些。但是我还没有找到任何方法。

【问题讨论】:

    标签: oracle plsql


    【解决方案1】:

    流水线函数的要点是提供 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 语法是在 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>
    

    【讨论】:

    • 感谢您的回复。但这对我的“问题”没有帮助,因为有了这个构造,我不仅有for ... loop ... end loop 构造,而且还有一个额外的bulk collect。我的意思是,这不是什么问题,因为我可以忍受它,但我认为它会在视觉上更具吸引力,通过简单的一行调用更多行而没有进一步的循环等等。
    【解决方案2】:

    试试 select column_value line from table(tq84_line.more_rows) 即在查询中包含包名。

    【讨论】:

    • select column_value line from table(more_rows) 工作正常。所需的替换没有。
    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多