oracle 集合操作

来源:互联网 发布:易云捷讯数据库 编辑:程序博客网 时间:2024/05/29 19:41

在pl sql 中,集合(collection) 是一组有序的元素组成的对象,这些元素的类型必须一致。

pl sql 将collection 分成3 类,分别为Associative arrays (也称index-by tables )、Nested tables、Varrays 。

Associative arrays ,可以看着是一个数据字典,有key,value 两列。key 值可以是任意数字和字符串,value 值可以是任意对象包括collection 类型的对象。

Nested tables ,可以看着是一个一维数组,可使用数字编号可以依次操作每个数组元素。

Varrays ,可以看着是一个预先已经定义好长度的一维数组,可使用数字编号可以依次操作每个数组元素。

Nested tables 和Varrays 可以做一个字段类型,将数据存储到数据库的表中。使用SQL 可以去操作它。所有的collection 都是一维的,但可以通过创建元素也是collection 的collection 对象来实现多维的collection 。

 

(miki西游 @mikixiyou 原文链接: http://mikixiyou.iteye.com/blog/1607889 )

 

一、操作collection 对象

所有的操作collection 对象的方法分别是COUNT, DELETE, EXISTS, EXTEND, FIRST,LAST, LIMIT, NEXT, PRIOR 和  TRIM 。

1 、这些方法在存储过程和函数中用于操作collection 对象,使用点语法调用。注意,他们都不能在SQL 语句中直接使用。

2 、extend  和trim 方法不能在Associative arrays 中使用;因为数据字典中根本不需要去扩展它的,当然也不知道怎么扩展。

3 、exists,count,limit,first,last,prior,next 是函数,有返回值的;

4 、extend,trim,delete 是存储过程,没有返回值,执行就执行了;

5 、exists,prior,next,trim,extend,delete 调用的参数对应于collection 的下标描述符,通常这些描述符都是数字,但是在associative arrays 中,有可能是字符窜。

6 、只有一个方法可以在  NULL 的collection 上可以被调用,范围boolean 类型的值。如果其他放在在  NULL 的collection 上调用后,会报 COLLECTION_IS_NULL 错误。

 

二、测试过程

Sql代码  收藏代码
  1. create or replace procedure sp_run_program as  
  2.   type typ_array is table of integer;  
  3.   type typ_dict is table of varchar2(100) index by varchar2(10);  
  4.   type typ_varray is varray(3) of varchar2(10);  
  5.   v_array  typ_array := typ_array();  
  6.   v_dict   typ_dict;  
  7.   v_varray typ_varray := typ_varray(nullnullnull);  
  8.   
  9. begin  
  10.   
  11.   v_array.extend(2);  
  12.   
  13.   dbms_output.put_line('The v_array''s count is ' || v_array.count);  
  14.   
  15.   v_array(1) := 1;  
  16.   for i in v_array.first .. v_array.last loop  
  17.     dbms_output.put_line('The v_array(' || i || ') is ' || v_array(i));  
  18.   end loop;  
  19.   
  20.   v_dict('one') := 'day';  
  21.   v_dict('two') := 'week';  
  22.   
  23.   dbms_output.put_line('The v_dict(''one'') is ' ||  
  24.                        nvl(v_dict('one'), 'null'));  
  25.   
  26.   dbms_output.put_line('The v_dict(''two'') is ' ||  
  27.                        nvl(v_dict('two'), 'null'));  
  28.   
  29.   v_varray(1) := 'a';  
  30.   v_varray(2) := 'b';  
  31.   
  32.   for i in v_varray.first .. v_varray.last loop  
  33.     dbms_output.put_line('The v_varray(' || i || ') is ' ||  
  34.                          nvl(v_varray(i), 'null'));  
  35.   end loop;  
  36.   
  37.   v_varray.trim(1);  
  38.   dbms_output.put_line('The v_varray trim(1)');  
  39.   
  40.   for i in v_varray.first .. v_varray.last loop  
  41.     dbms_output.put_line('The v_varray(' || i || ') is ' ||  
  42.                          nvl(v_varray(i), 'null'));  
  43.   end loop;  
  44.   v_varray.extend(1);  
  45.   dbms_output.put_line('The v_varray extend(1)');  
  46.   for i in v_varray.first .. v_varray.last loop  
  47.     dbms_output.put_line('The v_varray(' || i || ') is ' ||  
  48.                          nvl(v_varray(i), 'null'));  
  49.   end loop;  
  50.   
  51.   if (v_varray.EXISTS(4)) then  
  52.     dbms_output.put_line('The v_varray(4) is exists.');  
  53.   else  
  54.     dbms_output.put_line('The v_varray(4) is not exists.');  
  55.   end if;  
  56.   
  57. end sp_run_program;  
 

 

出的结果如下:

The v_array's count is 2

The v_array(1) is 1

The v_array(2) is

The v_dict('one') is day

The v_dict('two') is week

The v_varray(1) is a

The v_varray(2) is b

The v_varray(3) is null

The v_varray trim(1)

The v_varray(1) is a

The v_varray(2) is b

The v_varray extend(1)

The v_varray(1) is a

The v_varray(2) is b

The v_varray(3) is null

The v_varray(4) is not exists.


 

      使用collection,我实现一个计算器程序的开发,程序链接: http://mikixiyou.iteye.com/blog/1605823

 

简而言之,数量掌握collection  类型的使用方法,有助于我们开发出高质量的、复杂的应用程序。

原创粉丝点击