3

有两个链接 http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/composites.htm#LNPLS99981

在 Oracle 中使用不同类型的 PL/SQL 集合的目的

通过参考以上两个链接,我有两个疑问

1.哪一个是正确的嵌套表?

2.如果oracle doc是正确的,嵌套表和关联数组有什么区别?

4

2 回答 2

6

这是另一个不为人知的区别。您可以将两个嵌套表与=or<>但您不能的关联数组进行比较。

DECLARE

    TYPE associative_array IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
    a_var_associative_array associative_array;
    b_var_associative_array associative_array;

    TYPE nested_table IS TABLE OF INTEGER;
    a_var_nested_table nested_table := nested_table(1, 2, 3, 4, 5);
    b_var_nested_table nested_table := nested_table(5, 4, 3, 2, 1);

BEGIN

    IF a_var_nested_table = b_var_nested_table THEN
        -- Note, the different order of values!
        DBMS_OUTPUT.PUT_LINE ( 'TRUE' );
    ELSE
        DBMS_OUTPUT.PUT_LINE ( 'FALSE' );
    END IF;

    -- IF a_var_associative_array = b_var_associative_array THEN -> gives you an error! 

END;

当您使用嵌套表时,您还可以使用Multiset OperatorsMultiset ConditionsSET不适用于关联数组。

于 2014-12-03T12:28:00.667 回答
5

嵌套表只是一个包含 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 从碰巧是下一个和下一个和下一个的第一个索引中读取关联数组。

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。与批量收集一样元素的“名称”没有任何意义,您不会真正使用它们,而是通过如图所示的链。

于 2014-12-03T11:20:26.867 回答