Search code examples
oraclestored-procedurescollectionsplsqlvarray

Pass varray variables into stored procedure - basic


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('[email protected]','[email protected]','[email protected]');
 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.


Solution

  • 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('[email protected]','[email protected]','[email protected]');
        friends := friend_array('kwaja','sayya','mayya');
        update_address_book('1',friends,emails);
    end;
    /