wrong number or types of arguments in call to P_AA

喜欢而已 提交于 2020-06-17 14:04:25

问题


When I try to compile the procedure with collection type as in parameter, I'm getting the error like

wrong number or types of arguments in call to '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;
/


How can I fix my code?


回答1:


Your procedure defines the parameter like this:

serv in t45

So t45 is the defined datatype of the parameter.

Now when you call the procedure you pass in a variable v. And how is v defined?

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

t1 is a different type to t45. Even if they have identical structures they are different types. And that's why you get PLS-00306. The solution is quite simple: define v as t45.


i am getting error like 'Reference to uninitialized collection'.

You need to initialise the collection. You do this using the default constructor of the type, either at the start of the program ...

v := t45();

... or when you declare it:

v t45 := t45();

Once you get beyond that you will find your assignment logic is wrong: you're fetching into an element of the collection before you increment the counter or extend the array. So what you need is this:

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;
/ 

Alternatively use the less verbose bulk collect, which handles all the looping and type management implicitly :

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

Here is a db<>fiddle demo showing both approaches working.



来源:https://stackoverflow.com/questions/57542293/wrong-number-or-types-of-arguments-in-call-to-p-aa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!