oracle集合类型的first、next、prior、last方法.TXT

昨天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个元素的前一个元素的下标值(序列值)
这个是容易搞错的。

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26451536/viewspace-772241/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26451536/viewspace-772241/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值