昨天plsql challenge网站上的每日一题是关于goto语句、continue语句跳转用法的。选错了两个。发现自己对于集合的属性认识有点混淆,经常出错。通过以下的实验记录一下:
首先创建一个存储过程,用于产生oracle三种集合类型的数据
create or replace procedure plch_main
is
TYPE NESTED_T IS TABLE OF NUMBER;--嵌套表类型
TYPE ASSOCIATIVE_T IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; --索引表类型
TYPE VARRAYS_T IS VARRAY(10) OF NUMBER; --变长数组类型
lst_nested nested_t:=nested_t(-2000,null,3500);
lst_associative associative_t;--索引表类型的集合不能直接赋值 可以从其特性来理解(可以是稀疏排列的)
lst_varrays varrays_t:=varrays_t(-2000,null,3500);
begin
lst_associative(1):=-2000;
lst_associative(3):=null;--注意,这里没有lst_associative数组第二个元素
lst_associative(4):=3500;
dbms_output.put_line('以下为嵌套表的输出内容:');
dbms_output.put_line('lst_nested(1) :'||lst_nested(1));
dbms_output.put_line('lst_nested(2) :'||lst_nested(2));
dbms_output.put_line('lst_nested(3) :'||lst_nested(3));
dbms_output.put('lst_nested.first-1:');
dbms_output.put_line(lst_nested.first-1);
dbms_output.put_line('lst_nested.first :'||lst_nested.first);
dbms_output.put_line('lst_nested.prior(1):'||lst_nested.prior(1));
dbms_output.put_line('lst_nested.next(1):'||lst_nested.next(1));
dbms_output.put_line('lst_nested.next(2):'||lst_nested.next(2));
dbms_output.put_line('lst_nested.next(3):'||lst_nested.next(3));
dbms_output.put_line('lst_nested.last :'||lst_nested.last);
dbms_output.put_line('以下为索引表的输出内容:');
dbms_output.put_line('lst_associative(1) :'||lst_associative(1));
dbms_output.put_line('lst_associative(3) :'||lst_associative(3));
dbms_output.put_line('lst_associative(4) :'||lst_associative(4));
dbms_output.put('lst_associative.first-1:');
dbms_output.put_line(lst_associative.first-1);
dbms_output.put_line('lst_associative.first :'||lst_associative.first);
dbms_output.put_line('lst_associative.prior(1):'||lst_associative.prior(1));
dbms_output.put_line('lst_associative.next(1):'||lst_associative.next(1));
dbms_output.put_line('lst_associative.next(2):'||lst_associative.next(2));
dbms_output.put_line('lst_associative.next(4):'||lst_associative.next(4));
dbms_output.put_line('lst_associative.last :'||lst_associative.last);
dbms_output.put_line('以下为变长数组的输出内容:');
dbms_output.put_line('lst_varrays(1) :'||lst_varrays(1));
dbms_output.put_line('lst_varrays(2) :'||lst_varrays(2));
dbms_output.put_line('lst_varrays(3) :'||lst_varrays(3));
dbms_output.put('lst_varrays.first-1:');
dbms_output.put_line(lst_varrays.first-1);
dbms_output.put_line('lst_varrays.first :'||lst_varrays.first);
dbms_output.put_line('lst_varrays.prior(1):'||lst_varrays.prior(1));
dbms_output.put_line('lst_varrays.next(1):'||lst_varrays.next(1));
dbms_output.put_line('lst_varrays.next(2):'||lst_varrays.next(2));
dbms_output.put_line('lst_varrays.next(3):'||lst_varrays.next(3));
dbms_output.put_line('lst_varrays.last :'||lst_varrays.last);
end;
/
存储过程编译成功,以下调用并查看结果。
特别注意三种数组中第一个元素的prior方法、最后一个元素的next方法都是可以执行出来的;
并且索引表类型中未定义的元素序列也有prior、next方法。
SQL> exec plch_main;
以下为嵌套表的输出内容:
lst_nested(1) :-2000
lst_nested(2) :
lst_nested(3) :3500
lst_nested.first-1:0
lst_nested.first :1
lst_nested.prior(1):
lst_nested.next(1):2
lst_nested.next(2):3
lst_nested.next(3):
lst_nested.last :3
以下为索引表的输出内容:
lst_associative(1) :-2000
lst_associative(3) :
lst_associative(4) :3500
lst_associative.first-1:0
lst_associative.first :1
lst_associative.prior(1):
lst_associative.next(1):3
lst_associative.next(2):3
lst_associative.next(4):
lst_associative.last :4
以下为变长数组的输出内容:
lst_varrays(1) :-2000
lst_varrays(2) :
lst_varrays(3) :3500
lst_varrays.first-1:0
lst_varrays.first :1
lst_varrays.prior(1):
lst_varrays.next(1):2
lst_varrays.next(2):3
lst_varrays.next(3):
lst_varrays.last :3
PL/SQL procedure successfully completed
.first方法是数组中第一个元素的下标值(序列值)
.prior(n)方法是数组中第n个元素的前一个元素的下标值(序列值)
这个是容易搞错的。