What is the difference between nested array and associative array?

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

There are two links http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/composites.htm#LNPLS99981 and

Purpose of using different types of PL/SQL collections in Oracle

by referring above two links i have two doubt

1.Which one is correct nested table?

2.If the oracle doc is correct what is the difference between nested table and associative array?

回答1:

Here is another difference which is not that commonly known. You can compare two nested tables with = or <> but associative array you cannot.

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; 

When you work with nested tables you can also use Multiset Operators, Multiset Conditions and SET which are not available for associative arrays.



回答2:

A nested table is just an array of n elements.

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; 

A nested table must be initialized as shown. It has zero elements at first. To add elements we use EXTEND. This nested table has 10 elements. They are indexed 1 to 10. Element 1 has the value 100. The others have value null. An access to a non-existent element, say the 11th element, raises an error.

An associative array on the other hand is an array of name/value pairs. Let's use numbers (pls_integer typically) for the naming:

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; 

An associative array needs no initialization. It is empty and gets populated. Here we associate the element called 1 with the value 100 and the element with the name 11 with the value 1000. So there are two elements in the array. We get a no data found exception when we try to access a name that is not in the array.

We can also use strings for the names:

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; 

You can use both collections to get table data, but you use them differently. The nested table has a count and you can just loop from 1 to count to access its elements:

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; 

The associative array however must be read from whatever happens to be the first index to the next and next and next using FIRST and 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; 

The "names" happen to be 1, 2, 3, etc. here (given thus by the bulk collection) and you could access v_my_associative_array(1) for instance. Later in your program, however, after some possible delete operations in the array, there may be gaps, so you don't know whether an element named 1 exists and whether the element before element 4 happens to be element 3. As with bulk collect the "names" for the elements have no meaning you would not really use them, but go instead through the chain as shown.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!