简体   繁体   English

将关联数组传递给过程中的函数会抛出PLS-00382表达式类型错误

[英]Passing an associative array to a function within an procedure throws PLS-00382 expression is of wrong type

I have the following types: 我有以下几种类型:

CREATE OR REPLACE TYPE num_t AS OBJECT
(
  c1 number,
  MAP MEMBER FUNCTION sort_key RETURN VARCHAR2
)
CREATE OR REPLACE TYPE BODY num_t AS
   MAP MEMBER FUNCTION sort_key RETURN VARCHAR2 IS
   BEGIN
      RETURN c1;
   END;
END;

and

CREATE OR REPLACE TYPE num_tab AS TABLE OF num_t

My package is defined as follows: 我的包裹定义如下:

create or replace package test_package is

  type t_test is table of number index by binary_integer;
  test_empty_array t_test;

  procedure test_proc(cur_out out sys_refcursor);

  function test_fn(i_test in t_test) return num_tab;

end test_package;

create or replace package body test_package is

  procedure test_proc(cur_out out sys_refcursor) is
    i_test t_test := test_empty_array;
  begin
    open cur_out for
      select * from table(test_fn(i_test));

  end;

  function test_fn(i_test in t_test) return num_tab is
    v_results num_tab;
  begin
    for i in i_test.first .. i_test.last loop
      v_results.extend;
      v_results(i) := num_t(c1 => i_test(i));
    end loop;

    return v_results;
  end;

end test_package;

When I try to compile this, I get the following errors: 当我尝试对此进行编译时,出现以下错误:

Error: PLS-00382: expression is of wrong type
Line: 7
Text: select * from table(test_fn(i_test));

Error: PLS-00306: wrong number or types of arguments in call to 'TEST_FN'
Line: 7
Text: select * from table(test_fn(i_test));

Error: PL/SQL: ORA-00904: "TEST_PACKAGE"."TEST_FN": invalid identifier
Line: 7
Text: select * from table(test_fn(i_test));

Error: PL/SQL: SQL Statement ignored
Line: 7
Text: select * from table(test_fn(i_test));

It looks like it should work to me. 看来它应该对我有用。 Any ideas? 有任何想法吗?

this is normal and expected. 这是正常且预期的。 functions used in SQL can only reference SQL datatypes (eg you'd get an error if you had a boolean datatype too). SQL中使用的函数只能引用SQL数据类型(例如,如果您也具有布尔数据类型,则会出错)。

you can hide it in the package: 您可以将其隐藏在包装中:

create or replace package test_package is

  type t_test is table of number index by binary_integer;
  test_empty_array t_test;

  procedure test_proc(cur_out out sys_refcursor);

  function test_fn(i_test in t_test) return num_tab;

end test_package;
/
create or replace package body test_package is

  procedure test_proc(cur_out out sys_refcursor) is
    i_test t_test := test_empty_array;
    t_num  num_tab;
  begin
    t_num := test_fn(i_test);
    open cur_out for
      select * from table(t_num);

  end;

  function test_fn(i_test in t_test) return num_tab is
    v_results num_tab := num_tab();
  begin
    for i in 1..i_test.count loop
      v_results.extend;
      v_results(i) := num_t(c1 => i_test(i));
    end loop;

    return v_results;
  end;

end test_package;
/

i also tweaked 我也调整了

v_results num_tab := num_tab();

and

for 1..i_test.count loop

as first..last would fail in the case of a blank array (numeric error) 作为first..last将在空白数组的情况下失败(数字错误)

eg with some data: 例如带有一些数据:

SQL> create or replace package body test_package is
  2
  3    procedure test_proc(cur_out out sys_refcursor) is
  4      i_test t_test;
  5      t_num  num_tab;
  6    begin
  7      i_test(1) := 1;
  8      i_test(2) := 3;
  9      t_num := test_fn(i_test);
 10      open cur_out for
 11        select * from table(t_num);
 12
 13    end;
 14
 15    function test_fn(i_test in t_test) return num_tab is
 16      v_results num_tab := num_tab();
 17    begin
 18      for i in 1..i_test.count loop
 19        v_results.extend;
 20        v_results(i) := num_t(c1 => i_test(i));
 21      end loop;
 22
 23      return v_results;
 24    end;
 25
 26  end test_package;
 27  /

Package body created.

SQL> exec test_package.test_proc(:c)

PL/SQL procedure successfully completed.

SQL> print c

        C1
----------
         1
         3

SQL>

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

相关问题 为什么是PLS-00382:表达式的类型错误? - Why PLS-00382: expression is of wrong type? PLS-00382:Oracle游标中的表达式类型错误 - PLS-00382: expression is of wrong type in Oracle cursor Function Oracle PL SQL 批量收集错误 PLS-00382:表达式类型错误 - Function Oracle PL SQL bulk collect error PLS-00382: expression is of wrong type PLS-00382:表达式类型错误,无法显示 DBMS_OUTPUT.PUT_LINE - PLS-00382: expression is of wrong type, can't show DBMS_OUTPUT.PUT_LINE 在PL / SQL函数中使用UDT时的PLS-00382 - PLS-00382 when using a UDT in PL/SQL function 将Timestamp类型的关联数组传递给oracle存储过程 - passing associative array of type Timestamp to oracle stored procedure Oracle 函数编译成功但在执行 PLS-00221 时抛出错误:不是过程或未定义 - Oracle function compiles successfully but throws error while executing PLS-00221: is not a procedure or is undefined PLS-00320: 创建过程时此表达式的类型声明不完整或格式错误 - PLS-00320: the declaration of the type of this expression is incomplete or malformed while creating Procedure 为什么我会收到PLS-00382? - Why am I getting PLS - 00382? 将复合类型的数组传递给存储过程 - Passing array of a composite type to stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM