Oracle 11g Release 1 (11.1) PL/SQL_多维 Collection 类型和其异常,虽然 collection 只有一维的,但可以模型一个多维的。创建一
本文内容
多维 Collection
虽然 collection 只有一维的,但可以模型一个多维的。创建一个 collection,其每个元素也是 collection 。例如,,创建一个 varray 的 nested table,一个 varray 的 varray,一个 nested table 的 varray 等。
示例1:演示多维 varray
DECLARE TYPE t1 IS VARRAY(10) OF INTEGER; TYPE nt1 IS VARRAY(10) OF t1; -- multilevel varray type va t1 := t1(2,3,5); -- initialize multilevel varray nva 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 nva nva.EXTEND; -- replace inner varray elements nva(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
VARCHAR2(20); TYPE Ntb1 tb1; 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 elementsBEGIN vntb1.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
PLS_INTEGER; tb1 va1 INDEX BY PLS_INTEGER; v1 va1 := va1('hello', 'world'); v2 ntb1; v3 ntb2; v4 tb1; v5 tb1; -- empty tableBEGIN v4(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 again v2(35) := v5; v2(35)(2) := 78; -- it works nowEND;/