`
mikixiyou
  • 浏览: 1086581 次
  • 性别: Icon_minigender_1
  • 来自: 南京
博客专栏
C3c8d188-c0ab-3396-821d-b68331e21226
Oracle管理和开发
浏览量:349477
社区版块
存档分类
最新评论

PLSQL集合类型的使用总结

阅读更多

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

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

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

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

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

Nested tables Varrays 可以做一个字段类型,将数据存储到数据库的表中。使用SQL 可以去操作它。所有的collection 都是一维的,但可以通过创建元素也是collectioncollection 对象来实现多维的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 、只有一个方法可以在  NULLcollection 上可以被调用,范围boolean 类型的值。如果其他放在在  NULLcollection 上调用后,会报 COLLECTION_IS_NULL 错误。

 

二、测试过程

create or replace procedure sp_run_program as
  type typ_array is table of integer;
  type typ_dict is table of varchar2(100) index by varchar2(10);
  type typ_varray is varray(3) of varchar2(10);
  v_array  typ_array := typ_array();
  v_dict   typ_dict;
  v_varray typ_varray := typ_varray(null, null, null);

begin

  v_array.extend(2);

  dbms_output.put_line('The v_array''s count is ' || v_array.count);

  v_array(1) := 1;
  for i in v_array.first .. v_array.last loop
    dbms_output.put_line('The v_array(' || i || ') is ' || v_array(i));
  end loop;

  v_dict('one') := 'day';
  v_dict('two') := 'week';

  dbms_output.put_line('The v_dict(''one'') is ' ||
                       nvl(v_dict('one'), 'null'));

  dbms_output.put_line('The v_dict(''two'') is ' ||
                       nvl(v_dict('two'), 'null'));

  v_varray(1) := 'a';
  v_varray(2) := 'b';

  for i in v_varray.first .. v_varray.last loop
    dbms_output.put_line('The v_varray(' || i || ') is ' ||
                         nvl(v_varray(i), 'null'));
  end loop;

  v_varray.trim(1);
  dbms_output.put_line('The v_varray trim(1)');

  for i in v_varray.first .. v_varray.last loop
    dbms_output.put_line('The v_varray(' || i || ') is ' ||
                         nvl(v_varray(i), 'null'));
  end loop;
  v_varray.extend(1);
  dbms_output.put_line('The v_varray extend(1)');
  for i in v_varray.first .. v_varray.last loop
    dbms_output.put_line('The v_varray(' || i || ') is ' ||
                         nvl(v_varray(i), 'null'));
  end loop;

  if (v_varray.EXISTS(4)) then
    dbms_output.put_line('The v_varray(4) is exists.');
  else
    dbms_output.put_line('The v_varray(4) is not exists.');
  end if;

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  类型的使用方法,有助于我们开发出高质量的、复杂的应用程序。

 

 

 

 

3
7
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics