简体   繁体   English

如何在 with 语句中定义流水线表 function?

[英]How to define a pipelined table function within a with statement?

It's possible to defined a function within a with statement ( How to define a function and a query in the same with block? ).可以在 with 语句中定义 function( How to define a function and a query in the same with block? )。 But I fail to define a pipelined function inside a with statement.但是我无法在 with 语句中定义流水线 function。

WITH
    FUNCTION f (a IN INTEGER)
        RETURN SYS.odcinumberlist
        PIPELINED
    IS
        ret   INTEGER;
    BEGIN
        FOR z IN 1 .. a
        LOOP
            PIPE ROW (z);
        END LOOP;

        RETURN;
    END;
SELECT * FROM f(3);  --I tried with table(f(3)) too

[Error] Execution (2: 1): ORA-06553: PLS-653: aggregate/table functions are not allowed in PL/SQL scope [错误] 执行 (2:1): ORA-06553: PLS-653: PL/SQL scope 中不允许聚合/表函数

Is it possible to define a pipelined table function within a with statement and how?是否可以在 with 语句中定义流水线表 function 以及如何定义?

If it is possible, I would like to do that with a table of record.如果可能的话,我想用记录表来做到这一点。 Therefore I need to know how defined type within a with statement too.因此,我也需要知道如何在 with 语句中定义类型。

Within a subquery-factoring clause, you can make a non-pipelined function that returns a collection:在子查询分解子句中,您可以创建一个返回集合的非流水线 function:

WITH FUNCTION f (a IN INTEGER)
  RETURN SYS.ODCINUMBERLIST
IS
  v_list SYS.ODCINUMBERLIST := SYS.ODCINUMBERLIST();
BEGIN
  FOR z IN 1 .. a LOOP
    v_list.EXTEND;
    v_list(v_list.COUNT) := z;
  END LOOP;
  RETURN v_list;
END f;
SELECT *
FROM   TABLE(f(3)); 

Which outputs:哪些输出:

COLUMN_VALUE COLUMN_VALUE
1 1个
2 2个
3 3个

db<>fiddle here db<> 在这里摆弄


If it is possible, I would like to do that with a table of record.如果可能的话,我想用记录表来做到这一点。

A record is a PL/SQL data type;记录是一种 PL/SQL 数据类型; you cannot use it in an SQL statement.您不能在 SQL 语句中使用它。 Instead use an OBJECT data type and declare it in the global SQL scope before you run your query (no, you cannot declare it locally inside your query).而是使用OBJECT数据类型并在运行查询之前在全局 SQL scope 中声明它(不,您不能在查询中本地声明它)。

Therefore I need to know how defined type within a with statement too.因此,我也需要知道如何在 with 语句中定义类型。

Again, you cannot as the SQL syntax does not allow it.同样,您不能这样做,因为 SQL 语法不允许这样做。 See the answer to your previous question .请参阅上一个问题的答案。

You can declare it globally:您可以全局声明它:

CREATE TYPE test_obj IS OBJECT (
  w1 INTEGER
);

CREATE TYPE test_obj_table IS TABLE OF test_obj;

WITH FUNCTION f (a IN INTEGER)
  RETURN test_obj_table
IS
  v_list test_obj_table := test_obj_table();
BEGIN
  FOR z IN 1 .. a LOOP
    v_list.EXTEND;
    v_list(v_list.COUNT) := test_obj(z);
  END LOOP;
  RETURN v_list;
END f;
SELECT *
FROM   TABLE(f(3)); 

Outputs:输出:

W1 W1
1 1个
2 2个
3 3个

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM