如何使用 varray 输入测试 PL/SQL 过程

时间:2021-05-08 08:21:28

标签: sql testing plsql procedure

我制作了一个使用 varray 作为输入的过程,虽然一切都正确编译,但我无法弄清楚如何准确地测试/执行实际工作的过程。 varray 至少接受两个值:第一个是事件 ID (eid),之后的任何其他值都是个人 ID (pid)。两者都是最多 38 位的数字。

我习惯在结尾有一个声明,比如 exec event_attendees(3,9);工作正常,但它给了我一个错误。

Screenshot of error message from running this statement

我想制作不同的测试用例,以确保每个 if/else 部分都能正常工作(一个具有无效的 eid,一个具有无效的 pid,一个具有 eid 和 pid 组合已经是表的一部分,以及一个应该不会导致错误的)。为什么我尝试的 exec 语句不起作用,如何在我的 varray 中测试多个不同的值?

这是我目前所拥有的:

set serveroutput on;
create or replace type pe_varray as varray(10) of number(38);
/

create or replace procedure event_attendees(combo pe_varray)
is
  eid_valid_check int;
  pid_valid_check int;
  combo_exist_check int;
begin
  select count(*) into eid_valid_check from event where eid = combo(1);
  if eid_valid_check = 0 then
    dbms_output.put_line('Event does not exist');
  else
    for i in 2..combo.count loop
      select count(*) into pid_valid_check from people where pid = combo(i);
      if pid_valid_check = 0 then
        dbms_output.put_line('Person does not exist');
      else
        select count(*) into combo_exist_check from Person_Event where eid = combo(1) and pid = combo(i);
        if combo_exist_check > 0 then
          dbms_output.put_line('No need to insert');
        else
          insert into Person_Event values(combo(1), combo(i));
          dbms_output.put_line('Attendee(s) for event with id ' || combo(1) || ' added');
        end if;
      end if;
    end loop;
  end if;
end;

1 个答案:

答案 0 :(得分:1)

您需要先构造一个 VARRAY。您可以使用不同的值重新构造它并重新调用您的存储过程。 请尝试以下操作。

declare
  l_param pe_varray;
begin
  l_param := pe_varray(3, 9);
  event_attendees(l_param);
  l_param := pe_varray(0, -5);
  event_attendees(l_param);
end;