当前位置:数据库 > SQLServer >>

PLSQL集合类型的使用总结

PLSQL集合类型的使用总结
 
在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 。
 
一、操作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 错误。
 
二、测试过程
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.

 


Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,