Collection 之 associative array(index-by table)


 在oracle中我们常常会使用到集合,在PLSQL中可能会使用游标来保存一些集合数据,但是如果有比较复杂的业务,需要对集合中的数据多次使用或作为变量进行其他的业务处理,使用游标会感觉有些束缚,我们常常会使用PLSQL提供的集合类型。

 我们先来看一下介绍:

PL/SQL has three collection types—associative array,  VARRAY (variable-size array), and nested table.  They are similarities and differences 
这句话的意思是说PLSQL里面有3种类型的集合:关联数组,定长数组,嵌套表

今天我们主要看一下 Associative array,这是一个以key-value形式存储数据的集合,如果做过java开发的朋友应该很了解这种类型,和java中的HashMap用法一样

An associative array  (formerly called  PL/SQL table or  index-by table ) is a set of  key-value pairs. Each key is a unique index, used to locate the associated value with  the syntax  variable_name (index )

      关联数组通常被称为PLSQL表或者索引表,以键-值形式存储,并且有键的值只能有一个,(如果多次给一个键赋值,他不会报错,只记录最后一次赋值的value)

赋值语法:v_name(key):=value;


e.g1

SQL> 
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(1);
  4  l_test l_as_array;
  5  begin
  6  l_test('a'):='hello world!!!';  --赋值
  7  dbms_output.put_line(l_test('a'));  --取值
  8* end;
SQL> /
hello world!!!
PL/SQL procedure successfully completed.


上面演示也一个简单的关联数的赋值和取值,下面我们来看一下他的遍历

e.g2
SQL> ed
Wrote file afiedt.buf
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(1);
  4  l_test l_as_array;
  5  i varchar2(20);
  6  begin
  7  l_test('a'):='hello world1';
  8  l_test('b'):='hello world2';
  9  l_test('c'):='hello world3';
 10  i:=l_test.first;  --获取第一组数据的KEY
 11  while i is not null loop
 12    dbms_output.put_line(i||'===>'||l_test(i));
 13    i:=l_test.NEXT(i); --获取下一个key
 14  end loop;
 15* end;
SQL> /
a===>hello world1
b===>hello world2
c===>hello world3
PL/SQL procedure successfully completed.



对于重复值的处理

e.g3
SQL> ed
Wrote file afiedt.buf
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(1);
  4  l_test l_as_array;
  5  i varchar2(20);
  6  begin
  7  l_test('a'):='hello world1';
  8  l_test('a'):='hello world2';
  9  l_test('a'):='hello world3';
 10  dbms_output.put_line(l_test('a'));
 11* end;
SQL> /
hello world3
PL/SQL procedure successfully completed.

对于null值的处理
e.g4
SQL> ed
Wrote file afiedt.buf
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(4);
  4  l_test l_as_array;
  5  i varchar2(20);
  6  begin
  7  l_test('a'):=null;
  8  l_test('null'):='hello world2';
  9  l_test(null):='hello world3';
 10  dbms_output.put_line(l_test('a'));
 11  dbms_output.put_line(l_test('null'));
 12* end;
SQL> /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: NULL index table key value                     --这里说明不允许出现啊key为NULL
ORA-06512: at line 9

去掉之后:
SQL> ed
Wrote file afiedt.buf
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(4);
  4  l_test l_as_array;
  5  i varchar2(20);
  6  begin
  7  l_test('a'):=null;
  8  l_test('null'):='hello world2';
  9  dbms_output.put_line(l_test('a'));
 10  dbms_output.put_line(l_test('null'));
 11* end;
SQL> / 
--其实key为a的打印了只不过是个空字符串
hello world2  --null可以作为字符串
PL/SQL procedure successfully completed.



简单解释一下:
 type l_as_array is table of varchar2(20) 
 index by varchar2(4); 
这是PLSQL中自定义类型的语法,不仅关联数组如此。
这里需要注意的是:只能使用String(varchar,varchar2),PLS_INTEGER,Binary_Integer


同时index by table 包含的属性:

 在oracle中我们常常会使用到集合,在PLSQL中可能会使用游标来保存一些集合数据,但是如果有比较复杂的业务,需要对集合中的数据多次使用或作为变量进行其他的业务处理,使用游标会感觉有些束缚,我们常常会使用PLSQL提供的集合类型。

 我们先来看一下介绍:

PL/SQL has three collection types—associative array,  VARRAY (variable-size array), and nested table.  They are similarities and differences 
这句话的意思是说PLSQL里面有3种类型的集合:关联数组,定长数组,嵌套表

今天我们主要看一下 Associative array,这是一个以key-value形式存储数据的集合,如果做过java开发的朋友应该很了解这种类型,和java中的HashMap用法一样

An associative array  (formerly called  PL/SQL table or  index-by table ) is a set of  key-value pairs. Each key is a unique index, used to locate the associated value with  the syntax  variable_name (index )

      关联数组通常被称为PLSQL表或者索引表,以键-值形式存储,并且有键的值只能有一个,(如果多次给一个键赋值,他不会报错,只记录最后一次赋值的value)

赋值语法:v_name(key):=value;


e.g1

SQL> 
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(1);
  4  l_test l_as_array;
  5  begin
  6  l_test('a'):='hello world!!!';  --赋值
  7  dbms_output.put_line(l_test('a'));  --取值
  8* end;
SQL> /
hello world!!!
PL/SQL procedure successfully completed.


上面演示也一个简单的关联数的赋值和取值,下面我们来看一下他的遍历

e.g2
SQL> ed
Wrote file afiedt.buf
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(1);
  4  l_test l_as_array;
  5  i varchar2(20);
  6  begin
  7  l_test('a'):='hello world1';
  8  l_test('b'):='hello world2';
  9  l_test('c'):='hello world3';
 10  i:=l_test.first;  --获取第一组数据的KEY
 11  while i is not null loop
 12    dbms_output.put_line(i||'===>'||l_test(i));
 13    i:=l_test.NEXT(i); --获取下一个key
 14  end loop;
 15* end;
SQL> /
a===>hello world1
b===>hello world2
c===>hello world3
PL/SQL procedure successfully completed.



对于重复值的处理

e.g3
SQL> ed
Wrote file afiedt.buf
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(1);
  4  l_test l_as_array;
  5  i varchar2(20);
  6  begin
  7  l_test('a'):='hello world1';
  8  l_test('a'):='hello world2';
  9  l_test('a'):='hello world3';
 10  dbms_output.put_line(l_test('a'));
 11* end;
SQL> /
hello world3
PL/SQL procedure successfully completed.

对于null值的处理
e.g4
SQL> ed
Wrote file afiedt.buf
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(4);
  4  l_test l_as_array;
  5  i varchar2(20);
  6  begin
  7  l_test('a'):=null;
  8  l_test('null'):='hello world2';
  9  l_test(null):='hello world3';
 10  dbms_output.put_line(l_test('a'));
 11  dbms_output.put_line(l_test('null'));
 12* end;
SQL> /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: NULL index table key value                     --这里说明不允许出现啊key为NULL
ORA-06512: at line 9

去掉之后:
SQL> ed
Wrote file afiedt.buf
  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(4);
  4  l_test l_as_array;
  5  i varchar2(20);
  6  begin
  7  l_test('a'):=null;
  8  l_test('null'):='hello world2';
  9  dbms_output.put_line(l_test('a'));
 10  dbms_output.put_line(l_test('null'));
 11* end;
SQL> / 
--其实key为a的打印了只不过是个空字符串
hello world2  --null可以作为字符串
PL/SQL procedure successfully completed.



简单解释一下:
 type l_as_array is table of varchar2(20) 
 index by varchar2(4); 
这是PLSQL中自定义类型的语法,不仅关联数组如此。
这里需要注意的是:只能使用String(varchar,varchar2),PLS_INTEGER,Binary_Integer

它自身也有一些属性:
COUNT  总记录数
FIRST     返回最小的元素值
LAST 返回最大的元素值
PRIOR   返回输入元素的上一个元素
NEXT     返回输入元素的下一个元素


e.g5
SQL> ed
Wrote file afiedt.buf

  1  declare
  2  type l_as_array is table of varchar2(20)
  3  index by varchar2(1);
  4  l_test l_as_array;
  5  i varchar2(20);
  6  begin
  7  l_test('c'):='hello world  1';
  8  l_test('a'):='hello world  3';
  9  l_test('b'):='hello world  2';
 10  dbms_output.put_line('最小的元素==>'||l_test.FIRST);
 11  dbms_output.put_line('最大的元素==>'||l_test.LAST);
 12  l_test('1'):='hello world  c';
 13  l_test('2'):='hello world  b';
 14  l_test('3'):='hello world  a';
 15  dbms_output.put_line('最小的元素==>'||l_test.FIRST);
 16  dbms_output.put_line('最大的元素==>'||l_test.LAST);
 17  dbms_output.put_line('总记录数===>'||l_test.count);
 18  dbms_output.put_line('最小的元素==>'||l_test.NEXT(l_test.FIRST));
 19  dbms_output.put_line('元素的一个元素==>'||l_test.PRIOR(l_test.NEXT(l_test.FIRST)));
 20* end;
SQL> /
最小的元素==>a 这里最小的意思是将所有的key进行排序然后 当我key只有abc的时候,a最小
最大的元素==>c c最大
最小的元素==>1 当我加入123之后,数字的排序在字母之前,所以最小是1
最大的元素==>c 最大还是c
总记录数===>6
最小的元素==>2
元素的一个元素==>1
PL/SQL procedure successfully completed.


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

转载于:http://blog.itpub.net/27006877/viewspace-749782/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值