PL / SQL中的Oracle多级集合

ora*_*anj 0 oracle collections plsql

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

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;
Run Code Online (Sandbox Code Playgroud)

/

本质上,结构是:

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

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

Ale*_*ole 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');
Run Code Online (Sandbox Code Playgroud)

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

然后对于第二个学生,扩展并再次填充:

  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');
Run Code Online (Sandbox Code Playgroud)

要取回数据,可以遍历l_students条目:

  for i_stud in l_students.first..l_students.last
  loop
    ...
  end loop;
Run Code Online (Sandbox Code Playgroud)

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

   i_addr := l_students(i_stud).l_addr_entry.first;
Run Code Online (Sandbox Code Playgroud)

然后循环,递增该键值:

    loop
      ...
      i_addr := l_students(i_stud).l_addr_entry.next(i_addr);
    end loop;
Run Code Online (Sandbox Code Playgroud)

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

      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;
Run Code Online (Sandbox Code Playgroud)

因此,将它们放在一起,然后将值转储为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;
/
Run Code Online (Sandbox Code Playgroud)

得到:

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.
Run Code Online (Sandbox Code Playgroud)

db <>小提琴