简体   繁体   English

将Oracle PL / SQL数组输入到流水线函数的参数中

[英]Oracle PL/SQL array input into parameter of pipelined function

I am new to PL/SQL. 我是PL / SQL的新手。 I have created a pipelined function inside a package which takes as its parameter input an array of numbers (nested table). 我在一个包内创建了一个流水线函数,该函数将一个数字数组(嵌套表)作为其参数输入。

But I am having trouble trying to run it via an sql query. 但是我无法尝试通过sql查询运行它。 Please see below 请看下面

my input array 我的输入数组

 CREATE OR REPLACE TYPE num_array is TABLE of number;

my function declaration 我的函数声明

CREATE OR REPLACE PACKAGE "my_pack" as
    TYPE myRecord is RECORD(column_a NUMBER);
    TYPE myTable IS TABLE of myRecord;

    FUNCTION My_Function(inp_param num_array) return myTable PIPELINED;

end my_pack;

my function definition 我的功能定义

CREATE OR REPLACE PACKAGE BODY "my_pack" as

FUNCTION My_Function(inp_param num_array) return myTable PIPELINED as
        rec myRecord;
    BEGIN

        FOR i in 1..inp_param.count LOOP
            FOR e IN 
                (
                   SELECT column_a FROM  table_a where id=inp_param(i)

                )
                LOOP
                rec.column_a := e.column_a;

                PIPE ROW (rec); 

            END LOOP;
        END LOOP;

    RETURN;
END;

end my_pack;

Here is the latest code I've tried running from toad. 这是我尝试从蟾蜍运行的最新代码。 But it doesn't work 但这不起作用

declare
    myarray num_array;
    qrySQL varchar2(4000);
begin
    myarray := num_array(6341,6468);
    qrySQL := 'select * from TABLE(my_pack.My_Function(:myarray))';
    execute immediate qrySQL;
end;

So my question is how can I feed an array into this pipelined function from either TOAD or SQL Developer. 所以我的问题是如何从TOAD或SQL Developer向该流水线函数提供数组。 An example would be really handy. 一个例子真的很方便。

Thanks 谢谢

The error is fairly clear, you have a bind variable that you haven't assigned anything to. 该错误非常清楚,您有一个绑定变量,没有分配任何东西。 You need to pass your actual array with: 您需要通过以下方式传递实际数组:

qrySQL := 'select * from TABLE(my_pack.My_Function(:myarray))';
execute immediate qrySQL using myarray;

It's maybe more useful, if you want to call it from PL/SQL, to use static SQL as a cursor: 如果要从PL / SQL调用它,则将静态SQL用作游标可能会更有用:

set serveroutput on
declare
    myarray num_array;
begin
    myarray := num_array(6341,6468);
    for r in (select * from TABLE(my_pack.My_Function(myarray))) loop
      dbms_output.put_line(r.column_a);
    end loop;
end;
/

Or just query it statically as a test, for fixed values: 或者只是作为测试静态查询它,以获取固定值:

select * from TABLE(my_pack.My_Function(num_array(6341,6468)));

SQL Fiddle with some minor tweaks to the function to remove errors I think came from editing to post. SQL Fiddle对功能进行了一些细微调整,以消除我认为来自编辑和发布的错误。

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

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