PL / SQL中的Oracle多级集合

时间:2019-05-13 16:45:30

标签: oracle plsql collections

我有一个如下定义的多级集合。

declare
     type t_addr_lines is varray(4) of varchar2(60);
     type t_addr_entry is table of t_addr_lines index by varchar2(10);
     type t_student    is record
     (
         last_name      varchar2(20),
         first_name     varchar2(20),
         l_addr_entry   t_addr_entry
     );

     type t_students is table of t_student; 

     l_students t_students;

     begin

         l_students := t_students();
         l_students.extend();

     end;

/

基本上结构是:

 a. a student can have different types of addresses ( 'HOME', 'VACATION' )
 b. each address can have maximum of 4 lines

我想知道如何处理和填充集合的不同组成部分。

1 个答案:

答案 0 :(得分:5)

您正在混合几种收集类型,因此有点混乱,但是我想这就是重点。您可以通过其位置(数字)或使用第一个/最后一个来引用varray结构中的每个记录。然后直接将其分配给记录元素:

begin
  l_students := t_students();

  l_students.extend();
  -- explicit carray entry number 
  l_students(1).first_name := 'Bruce';
  l_students(1).last_name := 'Wayne';
  l_students(1).l_addr_entry('Home') := t_addr_lines('1007 Mountain Drive', 'Gotham');
  l_students(1).l_addr_entry('Work') := t_addr_lines('The Batcave', '1007 Mountain Drive', 'Gotham');

对于地址条目,您可以使用10个字符的值作为键来指定要分配的条目。家庭或工作。然后,您为t_addr_lines表分配一个新实例,该实例最多可填充for字符串。

然后让第二个学生扩展并再次填充:

  l_students.extend();
  -- last entry in varray
  l_students(l_students.last).first_name := 'Clark';
  l_students(l_students.last).last_name := 'Kent';
  l_students(l_students.last).l_addr_entry('Work') := t_addr_lines('The Daily Planet', 'Metropolis');

要获取数据,您可以遍历l_students项:

  for i_stud in l_students.first..l_students.last
  loop
    ...
  end loop;

地址有点棘手,特别是如果您想知道键值。您需要获取第一个键值,并将其分配给您必须先声明的变量:

   i_addr := l_students(i_stud).l_addr_entry.first;

然后循环,增加键值:

    loop
      ...
      i_addr := l_students(i_stud).l_addr_entry.next(i_addr);
    end loop;

然后在该循​​环(!)中,对该条目的地址行进行进一步循环:

      for i_line in l_students(i_stud).l_addr_entry(i_addr).first
                  ..l_students(i_stud).l_addr_entry(i_addr).last
      loop
        ...
      end loop;

所以把它们放在一起,然后用dbms_output转储值:

declare
  type t_addr_lines is varray(4) of varchar2(60);
  type t_addr_entry is table of t_addr_lines index by varchar2(10);

  type t_student    is record
  (
    last_name      varchar2(20),
    first_name     varchar2(20),
    l_addr_entry   t_addr_entry
  );

  type t_students is table of t_student; 

  l_students t_students;

  -- index for address entries
  i_addr varchar2(10);

begin
  l_students := t_students();

  l_students.extend();
  -- explicit carray entry number 
  l_students(1).first_name := 'Bruce';
  l_students(1).last_name := 'Wayne';
  l_students(1).l_addr_entry('Home') := t_addr_lines('1007 Mountain Drive', 'Gotham');
  l_students(1).l_addr_entry('Work') := t_addr_lines('The Batcave', '1007 Mountain Drive', 'Gotham');

  l_students.extend();
  -- last entry in varray
  l_students(l_students.last).first_name := 'Clark';
  l_students(l_students.last).last_name := 'Kent';
  l_students(l_students.last).l_addr_entry('Work') := t_addr_lines('The Daily Planet', 'Metropolis');

  for i_stud in l_students.first..l_students.last
  loop
    dbms_output.put_line('Student: '
      || l_students(i_stud).last_name ||', '|| l_students(i_stud).first_name);

    -- get index value of first address table entry
    i_addr := l_students(i_stud).l_addr_entry.first;
    -- loop over addresses starting from that index
    while i_addr is not null
    loop
      dbms_output.put_line('Address (' || i_addr || '):');
      -- loop over lines in this address
      for i_line in l_students(i_stud).l_addr_entry(i_addr).first
                  ..l_students(i_stud).l_addr_entry(i_addr).last
      loop
        dbms_output.put_line('  ' || l_students(i_stud).l_addr_entry(i_addr)(i_line));
      end loop;
      i_addr := l_students(i_stud).l_addr_entry.next(i_addr);
    end loop;
  end loop;
end;
/

得到:

Student: Wayne, Bruce
Address (Home):
  1007 Mountain Drive
  Gotham
Address (Work):
  The Batcave
  1007 Mountain Drive
  Gotham
Student: Kent, Clark
Address (Work):
  The Daily Planet
  Metropolis


PL/SQL procedure successfully completed.

db<>fiddle