【问题标题】:wrong number or types of arguments in call to P_AA调用 P_AA 时参数的数量或类型错误
【发布时间】:2019-12-23 19:41:42
【问题描述】:

当我尝试使用参数中的集合类型编译过程时,我收到类似

的错误

调用“P_AA”时参数的数量或类型错误

-------Procedure created with in parameter as nested table------------
create or replace procedure p_aa(serv in t45)
is
aa serv_item%rowtype;
begin
for i in 1..serv.count
loop
select a.* into aa from serv_item a where a.serv_item_id = serv(i);
dbms_output.put_line('Serv item   '||aa.serv_item_id||'      '||'status     '||aa.status);
end loop;
end;
/

----------Calling the package----------

declare
type t1 is table of number;
cursor c1 is select serv_item_id from serv_item;
n number:=0;
v t1;
begin
open c1;
loop
fetch c1 into v(n);
exit when c1%notfound;
n:=n+1;
end loop;
close c1;
p_aa(v);
end;
/


如何修复我的代码?

【问题讨论】:

    标签: oracle plsql collections procedure user-defined-types


    【解决方案1】:

    您的程序定义参数如下:

    serv in t45
    

    所以t45 是参数的定义数据类型。

    现在,当您调用该过程时,您传入一个变量v。 v 是如何定义的?

    type t1 is table of number;
    ...
    v t1;
    

    t1 是与t45 不同的类型。即使它们具有相同的结构它们是不同的类型。这就是您获得 PLS-00306 的原因。解决方法很简单:将v 定义为t45。


    我收到类似“对未初始化集合的引用”的错误。

    您需要初始化集合。您可以在程序开始时使用该类型的默认构造函数来执行此操作...

    v := t45();
    

    ...或者当你声明它时:

    v t45 := t45();
    

    一旦超出此范围,您就会发现分配逻辑是错误的:在递增计数器或扩展数组之前,您正在获取集合的元素。所以你需要的是这样的:

    declare
      cursor c1 is select serv_item_id from serv_item;
        n number:=0;
        v t45 := t45();
        x number;
    begin
      open c1;
      loop
        fetch c1 into x;
        exit when c1%notfound;
        n:=n+1;
        v.extend();
        v(n) := x;
      end loop;
      close c1;
      p_aa(v);
    end;
    / 
    

    或者使用不那么冗长的批量收集,它隐式处理所有循环和类型管理:

    declare
        v t45;
    begin
      select serv_item_id 
      bulk collect into v
      from serv_item;
      p_aa(v);
    end;
    /
    

    这里是a db<>fiddle demo,显示两种方法都有效。

    【讨论】:

    • 声明游标 c1 是 select serv_item_id from serv_item; n个数:=0; v t45;开始打开c1;循环 v.extend();取 c1 到 v(n) 中; c1%notfound 时退出; n:=n+1;结束循环;关闭 c1; p_aa(v);结尾; / 如果我将 t1 设为 t45,我会收到类似“对未初始化集合的引用”的错误。你能帮我解决这个问题吗
    猜你喜欢
    • 2020-08-10
    • 1970-01-01
    • 2021-07-15
    • 2018-03-22
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多