在pl/sql中,有三种集合类型:index-by tables(associative arrays,9i之前plsql table),nested table,arrays,它们都可以作为装载集合数据的载体,但有以下区别:
1. Nested table 和arrays 可以作为column,但index-by tables不可以
SQL> create type nested_type is table of varchar2(30);
2 /
Type created.
SQL> create type varray_type is varray(20) of varchar2(30);
2 /
Type created.
SQL> create table test(c1 varray_type,c2 nested_type) nested table c2 store as nested_type_tab;
Table created.
SQL> insert into test values(varray_type('aaa','bbb'),nested_type('ccc','ddd'));
1 row created.
2.index-by tables仅用于pl/sql,而Nested tables and arrays可用于pl/sql 和SQL
SQL> declare
2 type asso_type is table of varchar2(30) index by pls_integer;
3 lv_asso_type asso_type;
4 begin
5 lv_asso_type(1) := 'test';
6 dbms_output.put_line(lv_asso_type(1));
7 end;
8 /
test
PL/SQL procedure successfully completed.
SQL> declare
2 lv_nested_type nested_type :=nested_type();
3 lv_varray_type varray_type :=varray_type();
4
5 lv_nested_type2 nested_type :=nested_type();
6 lv_varray_type2 varray_type :=varray_type();
7 begin
8
9 lv_nested_type.extend(100);
10 lv_nested_type(100) :='test1';
11 dbms_output.put_line(lv_nested_type(100));
12
13 select lv_nested_type
14 into lv_nested_type2
15 from dual;
16
17 dbms_output.put_line(lv_nested_type2(100));
18
19
20 lv_varray_type.extend;
21 lv_varray_type(1) :='test2';
22 dbms_output.put_line(lv_varray_type(1));
23
24 select lv_varray_type
25 into lv_varray_type2
26 from dual;
27 dbms_output.put_line(lv_varray_type2(1));
28
29 end;
30 /
test1
test1
test2
test2
PL/SQL procedure successfully completed.