首页 > 解决方案 > PLSQL - 返回代码集合

问题描述

我正在尝试编写一个函数来返回代码列表。

在我声明的包装规格中

type codes_t is table of number;

function getCodes(
   acc_id in Account.id%type
)
return codes_t;

在我的身体里

function getCodes(
  acc_id in Account.id%type
)
return codes_t
is
  v_codes codes_t := codes_t();
begin
   for row in (select ...) loop
     v_codes.extend();
     v_codes(v_codes.count) := row.code;
   end loop;
return codes_t;

包编译器没有错误,但是当我尝试调用函数时

select pkg.getCodes(123) from dual;

我得到ORA-00902: 无效的数据类型

有什么明显的错误吗?

标签: plsql

解决方案


或者它可以是流水线的。 演示

CREATE OR REPLACE PACKAGE pkg
is

type codes_t is table of number;

function getCodes(
   acc_id in NUMBER
)
return codes_t PIPELINED;
end pkg;
/
CREATE OR REPLACE PACKAGE BODY pkg
is
  function getCodes(
    acc_id in NUMBER
  ) return codes_t PIPELINED
  is
    v_codes codes_t := codes_t();
  begin
     for r in (select acc_id as code from dual
                union 
                 select acc_id+1 from dual) loop
              PIPE row(r.code);
     end loop;
  end getCodes;
end pkg;
/

推荐阅读