-->示例1 DECLARE output VARCHAR2( 300 ); TYPE index_by_type IS TABLE OF VARCHAR2( 10 ) INDEX BY BINARY_INTEGER; index_by_table index_by_type; TYPE nested_type IS TABLE OF NUMBER; nested_table nested_type -->在声明块对嵌套表进行初始化并赋值 := nested_type( 10,20,30,40 ,50 ,60 ,70,80 ,90,100 ); BEGIN -- Populate index by table FOR i IN 1 .. 10 -->在执行块对联合数组赋值 LOOP index_by_table( i ) := 'Value_' || i; END LOOP; DBMS_OUTPUT. put_line( '--------------------------- Before deleted -----------------------------------------' ); FOR i IN index_by_table.FIRST .. index_by_table.LAST -->使用了first,last,作循环计数器上下标输出当前联合数组的所有元素 LOOP output := output || NVL( TO_CHAR( index_by_table( i ) ), 'NULL' ) || ' '; END LOOP; DBMS_OUTPUT.put_line( 'Element of Index_by_table are: ' || output ); output := ''; FOR i IN 1 .. nested_table.COUNT -->使用了count,作循环计数器上下标输出当前嵌套表的所有元素 LOOP output := output || NVL( TO_CHAR( nested_table( i ) ), 'NULL' ) || ' '; END LOOP; DBMS_OUTPUT.put_line( 'Element of nested_table are: ' || output ); IF index_by_table.EXISTS( 3 ) THEN -->EXISTS函数判断联合数组中的第3个元素是否存在 DBMS_OUTPUT.put_line( 'index_by_table(3) exists and the value is ' || index_by_table( 3 ) ); END IF; -- delete 10th element from a collection nested_table.delete( 10 ); -- delete elements 1 through 3 from a collection nested_table.delete( 1, 3 ); index_by_table.delete( 10 ); DBMS_OUTPUT.put_line( 'nested_table.COUNT = ' || nested_table.COUNT ); DBMS_OUTPUT.put_line( 'index_by_table.COUNT = ' || index_by_table.COUNT ); DBMS_OUTPUT.put_line( 'nested_table.FIRST = ' || nested_table.FIRST ); DBMS_OUTPUT.put_line( 'nested_table.LAST = ' || nested_table.LAST ); DBMS_OUTPUT.put_line( 'index_by_table.FIRST = ' || index_by_table.FIRST ); DBMS_OUTPUT.put_line( 'index_by_table.LAST = ' || index_by_table.LAST ); DBMS_OUTPUT.put_line( 'nested_table.PRIOR(2) = ' || nested_table.PRIOR( 2 ) ); DBMS_OUTPUT.put_line( 'nested_table.NEXT(2) = ' || nested_table.NEXT( 2 ) ); DBMS_OUTPUT.put_line( 'index_by_table.PRIOR(2) = ' || index_by_table.PRIOR( 2 ) ); DBMS_OUTPUT.put_line( 'index_by_table.NEXT(2) = ' || index_by_table.NEXT( 2 ) ); -- Trim last two elements nested_table.TRIM( 2 ); -- Trim last element nested_table.TRIM; DBMS_OUTPUT.put_line( 'nested_table.LAST = ' || nested_table.LAST ); DBMS_OUTPUT.put_line( '--------------------------- After deleted -----------------------------------------' ); output:=''; FOR i IN index_by_table.FIRST .. index_by_table.LAST -->输出删除元素后联合数组的所有剩余元素 LOOP output := output || NVL( TO_CHAR( index_by_table( i ) ), 'NULL' ) || ' '; END LOOP; DBMS_OUTPUT.put_line( 'Element of Index_by_table are: ' || output ); output := ''; output:=''; FOR i IN nested_table.FIRST .. nested_table.LAST -->输出删除元素后嵌套表的所有剩余元素 LOOP output := output || NVL( TO_CHAR( nested_table( i ) ), 'NULL' ) || ' '; END LOOP; DBMS_OUTPUT.put_line( 'Element of nested_table are: ' || output ); END; --------------------------- Before deleted ----------------------------------------- Element of Index_by_table are: Value_1 Value_2 Value_3 Value_4 Value_5 Value_6 Value_7 Value_8 Value_9 Value_10 Element of nested_table are: 10 20 30 40 50 60 70 80 90 100 index_by_table(3) exists and the value is Value_3 nested_table.COUNT = 6 -->嵌套表使用了两次delete,分别是删除最后一个元素和删除第1到第3个元素,因此嵌套表的count输出为6 index_by_table.COUNT = 9 -->联合数组中删除了最后的一个元素,因此联合数组的count输出为9 nested_table.FIRST = 4 -->嵌套表删除了第1到第3个元素,因此其first变成4 nested_table.LAST = 9 -->嵌套表删除了最后一个元素,因此last变成9 index_by_table.FIRST = 1 index_by_table.LAST = 9 nested_table.PRIOR(2) = -->嵌套表的PRIOR(2),第2个元素的前一个(下标为1),由于1-3都被删除,且1之前没有任何元素,故为NULL nested_table.NEXT(2) = 4 -->嵌套表2之后元素的下标,原本应该是3,由于3被删除,因此3被忽略,返回4 index_by_table.PRIOR(2) = 1 index_by_table.NEXT(2) = 3 nested_table.LAST = 7 -->nested_table.TRIM(2)与nested_table.TRIM总共删除了3个元素及占位符,故LAST为7。 --------------------------- After deleted ----------------------------------------- Element of Index_by_table are: Value_1 Value_2 Value_3 Value_4 Value_5 Value_6 Value_7 Value_8 Value_9 Element of nested_table are: 40 50 60 70 PL/SQL procedure successfully completed. ---------------------------------------------------------------------------------------------------------------------------- -->示例2 DECLARE TYPE varray_type IS VARRAY(10) OF NUMBER; varray varray_type := varray_type(1, 2, 3, 4, 5, 6); PROCEDURE print_numlist( the_list varray_type ) IS output VARCHAR2( 128 ); BEGIN FOR i IN the_list.FIRST .. the_list.LAST LOOP output := output || NVL( TO_CHAR( the_list( i ) ), 'NULL' ) || ' '; END LOOP; DBMS_OUTPUT.put_line( output ); END; BEGIN print_numlist( varray ); DBMS_OUTPUT.put_line( 'varray.COUNT = ' || varray.COUNT ); DBMS_OUTPUT.put_line( 'varray.LIMIT = ' || varray.LIMIT ); DBMS_OUTPUT.put_line( 'varray.FIRST = ' || varray.FIRST ); DBMS_OUTPUT.put_line( 'varray.LAST = ' || varray.LAST ); DBMS_OUTPUT.put_line( 'The maximum number you can use with ' || 'varray.EXTEND() is ' || ( varray.LIMIT - varray.COUNT ) ); varray.EXTEND( 2, 4 ); -->将第4个元素的值复制2份,追加到集合尾部 DBMS_OUTPUT.put_line( 'varray.LAST = ' || varray.LAST ); DBMS_OUTPUT.put_line( 'varray(' || varray.LAST || ') = ' || varray( varray.LAST ) ); print_numlist( varray ); -- Trim last two elements varray.TRIM( 2 ); DBMS_OUTPUT.put_line( 'varray.LAST = ' || varray.LAST ); END; 1 2 3 4 5 6 -->输出varray中的所有元素 varray.COUNT = 6 varray.LIMIT = 10 -->limit方法得到变长数组的最大容量 varray.FIRST = 1 varray.LAST = 6 The maximum number you can use with varray.EXTEND() is 4 -->得到可以extend的容量,即还可以保存4个元素 varray.LAST = 8 --> extend之后last的下标值为8 varray(8) = 4 -->第8个元素的值则为4 1 2 3 4 5 6 4 4 -->输出varray中的所有元素 varray.LAST = 6 -->由于使用了varray.TRIM( 2 ),所以last又变成了6 PL/SQL procedure successfully completed. -->Author : Robinson Cheng -->Blog : http://blog.csdn.net/robinson_0612
三、更多参考
PL/SQL 集合的初始化与赋值
PL/SQL 联合数组与嵌套表
PL/SQL 变长数组
PL/SQL --> PL/SQL记录
SQL tuning 步骤
高效SQL语句必杀技
父游标、子游标及共享游标
绑定变量及其优缺点
dbms_xplan之display_cursor函数的使用
dbms_xplan之display函数的使用
执行计划中各字段各模块描述
使用 EXPLAIN PLAN 获取SQL语句执行计划
启用 AUTOTRACE 功能
函数使得索引列失效
Oracle 绑定变量窥探
Oracle 自适应共享游标