嵌套表只是一个包含 n 个元素的数组。
declare
type nested_table_of_integer is table of integer;
v_my_nested_table nested_table_of_integer;
begin
v_my_nested_table := nested_table_of_integer(); -- initialize
v_my_nested_table.extend(10); -- add 10 elements
v_my_nested_table(1) := 100;
v_my_nested_table(11) := 1000; -- ORA-06533: Subscript beyond count
end;
嵌套表必须如图所示进行初始化。一开始它的元素为零。要添加元素,我们使用 EXTEND。这个嵌套表有 10 个元素。它们的索引为 1 到 10。元素 1 的值为 100。其他元素的值为 null。访问不存在的元素(例如第 11 个元素)会引发错误。
另一方面,关联数组是名称/值对的数组。让我们使用数字(通常为 pls_integer)来命名:
declare
type associative_array_of_integer is table of integer index by pls_integer;
v_my_associative_array associative_array_of_integer;
begin
v_my_associative_array(1) := 100;
v_my_associative_array(11) := 1000;
v_my_associative_array(12) := v_my_associative_array(2); -- ORA-01403: no data found
end;
关联数组不需要初始化。它是空的并且被填充。这里我们将名为 1 的元素与值 100 相关联,将名为 11 的元素与值 1000 相关联。所以数组中有两个元素。当我们尝试访问不在数组中的名称时,我们得到一个找不到数据的异常。
我们也可以使用字符串作为名称:
declare
type associative_array_of_integer is table of integer index by varchar2(100);
v_my_associative_array associative_array_of_integer;
begin
v_my_associative_array('age father') := 39;
v_my_associative_array('age mother') := 32;
v_my_associative_array('age daughter') := 11;
end;
您可以使用这两个集合来获取表数据,但使用方式不同。嵌套表有一个计数,你可以从 1 循环到计数来访问它的元素:
declare
type nested_table_of_integer is table of integer;
v_my_nested_table nested_table_of_integer;
begin
v_my_nested_table := nested_table_of_integer(); -- initialize
select table_name bulk collect into v_my_nested_table from user_tables;
for i in 1 .. v_my_nested_table.count loop
dbms_output.put_line(v_my_nested_table(i));
end loop;
end;
然而,关联数组必须使用 FIRST 和 NEXT 从恰好是 next 和 next 和 next 的第一个索引中读取。
declare
type associative_array_of_integer is table of integer index by pls_integer;
v_my_associative_array associative_array_of_integer;
i integer;
begin
select table_name bulk collect into v_my_associative_array from user_tables;
i := v_my_associative_array.first;
while i is not null loop
dbms_output.put_line(v_my_associative_array(i));
i := v_my_associative_array.next(i);
end loop;
end;
这里的“名称”恰好是 1、2、3 等(因此由批量集合给出),例如,您可以访问 v_my_associative_array(1)。然而,在你的程序后面,在数组中进行一些可能的删除操作之后,可能会出现间隙,所以你不知道是否存在名为 1 的元素,也不知道元素 4 之前的元素是否恰好是元素 3。与批量收集一样元素的“名称”没有任何意义,您不会真正使用它们,而是通过如图所示的链。