Pass varray variables into stored procedure - basic

余生颓废 提交于 2019-12-13 19:13:39

问题


Hello I am a php developer, trying to get going with Oracle. So I need to pass a collection of variables into an Oracle stored procedure. So as a basic try, I am trying to access a procedure which would accept three parameters, out of which two would be varrays, but when I pass the declared varrays, I am getting an error. I am pretty sure, it is something to do with a little syntax, but i am not able to figure out that thing.

Below is my table schema and stored procedure:

create table emails (
user_id varchar2(10),
friend_name varchar2(20),
email_address varchar2(20));

create or replace type email_array as varray(100) of varchar2(20);
/
show errors
create or replace type friend_array as varray(100) of varchar2(20);
/
show errors

create or replace procedure update_address_book(
p_user_id in varchar2,
p_friend_name friend_array,
p_email_addresses email_array)
is
begin
delete from emails where user_id = p_user_id;
forall i in indices of p_email_addresses
insert into emails (user_id, friend_name, email_address)
values (p_user_id, p_friend_name(i), p_email_addresses(i));
end update_address_book;

Now, below pasted is my the way I am trying to access this procedure from an anonymous block.

declare 
  type email_list is varray(100) of varchar2(20);
  type friend_list is varray(100) of varchar2(20);
  emails email_list;
  friends friend_list;
begin
 emails :=email_list('khwaja@gmail.com','sayya@gmail.com','mayya@gmail.com');
 friends := friend_list('kwaja','sayya','mayya');
 execute update_address_book('1',emails,friends);
end; 

The error I am getting is near the execute, I think I am not supposed to execute a procedure inside a declare block, but I am unable to understand how would I work around.


回答1:


You don't need the keyword EXECUTE to call the procedure from within a PL/SQL block. Just remove that word.

But it would be helpful if you showed the actual error(s) you're getting as there could be something else wrong. For example, you also have the parameters to the update_address_book() call in the wrong order, and you're recreating new types inside your block instead of using the ones declared earlier.

This will run:

declare 
    emails email_array;
    friends friend_array;
begin
    emails := email_array('khwaja@gmail.com','sayya@gmail.com','mayya@gmail.com');
    friends := friend_array('kwaja','sayya','mayya');
    update_address_book('1',friends,emails);
end;
/


来源:https://stackoverflow.com/questions/4892480/pass-varray-variables-into-stored-procedure-basic

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