本文内容
多维 Collection
虽然 collection 只有一维的,但可以模型一个多维的。创建一个 collection,其每个元素也是 collection 。例如,创建一个 varray 的 nested table,一个 varray 的 varray,一个 nested table 的 varray 等。
示例1:演示多维 varray
DECLARETYPE t1 IS VARRAY(10) OF INTEGER;
TYPE nt1 IS VARRAY(10) OF t1; -- multilevel varray type
va t1 := t1(2,3,5);
-- initialize multilevel varraynva nt1 := nt1(va, t1(55,6,73), t1(2,4), va);
i INTEGER;va1 t1;
BEGIN -- multilevel access i := nva(2)(3); -- i will get value 73 DBMS_OUTPUT.PUT_LINE('I = ' || i); -- add a new varray element to nvanva.EXTEND;
-- replace inner varray elementsnva(5) := t1(56, 32);
nva(4) := t1(45,43,67,43345);
-- replace an inner integer element nva(4)(4) := 1; -- replaces 43345 with 1 -- add a new element to the 4th varray element -- and store integer 89 into it.nva(4).EXTEND;
nva(4)(5) := 89;
END;/
示例2:演示多维 nested table
DECLARETYPE tb1 IS TABLE OF VARCHAR2(20);
TYPE Ntb1 IS TABLE OF tb1; -- table of table elements
TYPE Tv1 IS VARRAY(10) OF INTEGER;
TYPE ntb2 IS TABLE OF tv1; -- table of varray elements
vtb1 tb1 := tb1('one', 'three');
vntb1 ntb1 := ntb1(vtb1);
vntb2 ntb2 := ntb2(tv1(3,5), tv1(5,7,3));
-- table of varray elementsBEGINvntb1.EXTEND;
vntb1(2) := vntb1(1);
-- delete the first element in vntb1 vntb1.DELETE(1); -- delete the first string -- from the second table in the nested table vntb1(2).DELETE(1);END;/
示例3:演示多维 associative array
DECLARETYPE tb1 IS TABLE OF INTEGER INDEX BY PLS_INTEGER;
-- the following is index-by table of index-by tablesTYPE ntb1 IS TABLE OF tb1 INDEX BY PLS_INTEGER;
TYPE va1 IS VARRAY(10) OF VARCHAR2(20);
-- the following is index-by table of varray elementsTYPE ntb2 IS TABLE OF va1 INDEX BY PLS_INTEGER;
v1 va1 := va1('hello', 'world');
v2 ntb1;
v3 ntb2;
v4 tb1;
v5 tb1; -- empty tableBEGINv4(1) := 34;
v4(2) := 46456;
v4(456) := 343;
v2(23) := v4;
v3(34) := va1(33, 456, 656, 343);
-- assign an empty table to v2(35) and try againv2(35) := v5;
v2(35)(2) := 78; -- it works nowEND;/
