Oracle定义联合数组及使用技巧

联合数组以前被称为pl/sql表。在表中不能使用联合数组,只能将它们用作程序设计的结构体。只能在pl/sql中访问联合数组。

注意到联合数组带来的一些关键问题是非常重要的。这些问题使我们介绍它们的用法时,需要采取一些特别的方法。这些问题包括:

联合数组不需要初始化,也没有构造函数语法。在对它们进行赋值以前,也不需要专门为其分配存储空间,也就不需要使用集合api的extend方法。

在oracle 10g中,以及在oracle 10g以前的版本中,都可以使用数字索引联合数组。另外,在oracle 10g中,还可以使用具有唯一性的变长字符串作为联合数组的索引。

可以使用任意的整数作为联合数组的索引,这就说明联合数组的索引可以是任意正数、负数或0。

可以显式地将等价的%rowtype、记录类型和对象类型的返回值,转换成联合数组的结构体。

联合数组是使用forall语句或bulk collect子句的关键,而后者则允许数据库到程序设计单元的批转换。

在使用了全球化设置,例如nls_comp或nls_sort初始化参数的数据库中,将字符串用作联合数组索引的时候,需要我们进行特殊的处理。

1、定义联合数组和用作pl/sql的程序结构体

在pl/sql语言中定义联合数组的语法有两种,一种是:

create or replace type type_name

as table of element_type [not null]

index by [pls_integer | binary_integer | varchar2(size) ];

可以将正数、负数或者0值用作联合数组的索引。oracle 10g中的pls_integer何binary_integer类型都是不受限制的数据类型,这两个数据类型都映射到c/c++、c#和java的调用规范中。

变长字符串的最大长度为4000个字符。

另一种定义联合数组的语法是:

create or replace type type_name

as table of element_type [not null]

index by key_type;

其中的key_type允许我们使用varchar2、string或long类型。使用varchar2和string时,都需要定义大小。使用long类型时,则不需要定义大小,因为它是通过定义varchar(32760)进行定义的。

联合数组不需要进行初始化,也没有构造函数语法。这是与其他两种集合类型(varrays和嵌套表)有着本质区别的地方。

如果你像下面这样构造一个联合数组,那么会引发pls-00222异常。


复制代码 代码如下:

— define an associative array of strings.

type card_table is table of varchar2(5 char)

index by binary_integer;

— and attempt to construct an associative array.

cards card_table := card_table(‘a’,’b’,’c’);

begin

null;

end;

在前面的介绍中,我们知道对象的构造函数是完全可以作为一个函数使用的。其他集合类型,例如varrays和嵌套表,都是显式定义构造函数的对象类型。而联合数组只是一个结构体,不是一个对象类型。因此,它不能显式地创建构造函数,也无法调用构造函数。

2、联合数组的初始化

前面已经说过,我们可以将数字或者具有唯一性的变长字符串作为索引,构造联合数组。数字索引比如为整数,可以为正整数、负整数和0值。唯一性的变长字符串可以是varchar2、string或long数据类型。

1)以数字作为联合数组索引

下面的例子给出了一个向以数字为索引的联合数组中的元素赋值的过程,该示例示范了将varray的内容转移到联合数组的过程。


复制代码 代码如下:

— define a varray of twelve strings.

type months_varray is varray(12) of string(9 char);

— define an associative array of strings.

type calendar_table is table of varchar2(9 char)

index by binary_integer;

— and construct a varray.

month months_varray :=

months_varray(‘january’,’february’,’march’

,’april’,’may’,’june’

,’july’,’august’,’september’

,’october’,’november’,’december’);

— an associative array variable.

calendar calendar_table;

begin

— check if calendar has no elements.

if calendar.count = 0 then

— print a title

dbms_output.put_line(‘assignment loop:’);

dbms_output.put_line(‘—————-‘);

— loop through all the varray elements.

for i in month.first..month.last loop

— initialize a null associative array element.

calendar(i) := ”;

— print an indexed element from the associative array.

dbms_output.put_line(

‘index [‘||i||’] is [‘||calendar(i)||’]’);

— assign the numeric index valued varray element

— to an equal index valued associative array element.

calendar(i) := month(i);

end loop;

— print a title

dbms_output.put(chr(10));

dbms_output.put_line(‘post-assignment loop:’);

dbms_output.put_line(‘———————‘);

— loop through all the associative array elements.

for i in calendar.first..calendar.last loop

— print an indexed element from the associative array.

dbms_output.put_line(

‘index [‘||i||’] is [‘||calendar(i)||’]’);

end loop;

end if;

end;

/

在第一个for-loop循环中,用等于varray类型的month索引的一个索引值,为联合数组类型的calendar变量赋上一个空值。这是为联合数组分配空间的唯一方法。

2)以唯一字符串作为联合数组索引

如下例所示:


复制代码 代码如下:

— define a varray of twelve variable length strings.

type months_varray is varray(12) of string(9 char);

— define an associative array of variable length strings.

type calendar_table is table of varchar2(9 char)

index by varchar2(9 char);

— and construct a varray.

month months_varray :=

months_varray(‘january’,’february’,’march’

,’april’,’may’,’june’

,’july’,’august’,’september’

,’october’,’november’,’december’);

— an associative array variable.

calendar calendar_table;

begin

— check if calendar has no elements.

if calendar.count = 0 then

— print a title

dbms_output.put_line(‘assignment loop:’);

dbms_output.put_line(‘—————-‘);

— loop through all the varray elements.

for i in month.first..month.last loop

— assign the numeric index valued varray element

— to an equal index valued associative array element.

calendar(month(i)) := ”; –i;

— print an indexed element from the associative array.

dbms_output.put_line(

‘index [‘||month(i)||’] is [‘||i||’]’);

end loop;

— print a title

dbms_output.put(chr(10));

dbms_output.put_line(‘post-assignment loop:’);

dbms_output.put_line(‘———————‘);

— loop through all the associative array elements.

for i in calendar.first..calendar.last loop

— print an indexed element from the associative array.

dbms_output.put_line(

‘index [‘||i||’] is [‘||calendar(i)||’]’);

end loop;

end if;

end;

运行上面这段代码会出现错误。ora-06502:pl/sql:numeric or value error:character to number convertion error。在第一个for-loop中的初始化是没有任何问题的。可是在第二个for-loop循环中,程序试图向计数器变量传递一个非数字的值。在上面的程序中,这个计数器变量是i。计数器变量的数据类型被定义为pls_integer类型。所以,就不能将整个变长字符串的索引值赋给一个整型变量—因为变长字符串不是整数。这样,自然就引发了类型转换错误ora-06502。该示例之所以会引发错误,是因为在初始化联合数组成员的时候,其中的计数器变量被转换为varchar2类型,而在读联合数组的时候,又将该计数器类型转为integer类型。

这其实给我们提出了一个新问题。非数字索引值需要我们明确的知道索引的开始值以及索引的递增方法。集合api的first何next方法提供了这种工具。

如下例所示:


复制代码 代码如下:

— define variables to traverse an associative array that

— uses variable length strings for index values.

current varchar2(9 char);

element integer;

— define a varray of twelve variable length strings.

type months_varray is varray(12) of string(9 char);

— define an associative array of variable length strings.

type calendar_table is table of varchar2(9 char)

index by varchar2(9 char);

— and construct a varray.

month months_varray :=

months_varray(‘january’,’february’,’march’

,’april’,’may’,’june’

,’july’,’august’,’september’

,’october’,’november’,’december’);

— an associative array variable.

calendar calendar_table;

begin

— check if calendar has no elements.

if calendar.count = 0 then

— print a title

dbms_output.put_line(‘assignment loop:’);

dbms_output.put_line(‘—————-‘);

— loop through all the varray elements.

for i in month.first..month.last loop

— assign the numeric index valued varray element

— to an equal index valued associative array element.

calendar(month(i)) := to_char(i);

— print an indexed element from the associative array.

dbms_output.put_line(

‘index [‘||month(i)||’] is [‘||i||’]’);

end loop;

— print a title

dbms_output.put(chr(10));

dbms_output.put_line(‘post-assignment loop:’);

dbms_output.put_line(‘———————‘);

— loop through all the associative array elements.

for i in 1..calendar.count loop

— check if the first element in the loop.

if i = 1 then

— assign the first character index to a variable.

current := calendar.first;

— use the derived index to find the next index.

element := calendar(current);

else

— check if next index value exists.

if calendar.next(current) is not null then

— assign the character index to a variable.

current := calendar.next(current);

— use the derived index to find the next index.

element := calendar(current);

else

— exit loop since last index value is read.

exit;

end if;

end if;

— print an indexed element from the associative array.

dbms_output.put_line(

‘index [‘||current||’] is [‘||element||’]’);

end loop;

end if;

end;

3、与bulk collect和forall结合使用联合数组

使用bulk collect和forall胃我们打开了消除行级处理之门。使用bulk collect可以获取存储在联合数组或嵌套表中的记录集。使用forall可以成批的发送dml语句。forall可以插入、更新和删除数据。这些方法减少了pl/sql引擎和sql引擎之间来回切换上下文环境的次数。如果没有这些方法,就会有太多的解析或取值过程。

你应该还记得行级处理实际上使用的是%rowtype和%type。前者可以直接映射到记录类型上。bulk collect可以将%rowtype或%type类型的值的一个集合作为联合数组或嵌套表的一个集合进行赋值。forall提供了一种可以将联合数组或嵌套表中的内容转移到数据库对象的方法。

联合数组和嵌套表集合类型可以与bulk collect和forall结合使用。使用嵌套表时,需要将嵌套表构造为空元素的集合。bulk collect会显式地分配嵌套表的存储空间。不需要对联合数组进行构造,只要一个批赋值就可以了。同样,联合数组和嵌套表都可以作为sql命令forall的源结构。

如下示例所示:


复制代码 代码如下:

— create a table for the example.

create table bulk_numbers

(number_id number not null

,constraint number_id_pk primary key (number_id));

— define an associative array of integers.

type number_table is table of bulk_numbers.number_id%type

index by binary_integer;

— define a variable of the associative array type.

number_list number_table;

begin

— loop from 1 to a million and increment associative array.

for i in 1..10000 loop

— assign number value.

number_list(i) := i;

end loop;

— loop through all to do a bulk insert.

forall i in 1..number_list.count

insert

into bulk_numbers

values (number_list(i));

— commit records.

commit;

end;

— use a bulk collect to retrieve a table into an

— associative array.

— define an associative array of integers.

type number_table is table of bulk_numbers.number_id%type

index by binary_integer;

— define a variable of the associative array type.

number_list number_table;

begin

— check if calendar has no elements.

select number_id

bulk collect

into number_list

from bulk_numbers;

— print a title

dbms_output.put_line(‘bulk collected:’);

dbms_output.put_line(‘—————‘);

— loop through to print elements.

–只打印前两条和最后两条记录

for i in number_list.first..number_list.last loop

— print only the first and last two.

if i <= 2 or i >= 9999 then

— print an indexed element from the associative array.

dbms_output.put_line(‘number [‘||number_list(i)||’]’);

end if;

end loop;

end;

在bulk collect子句中使用了order by,保证得出的结果是按照数字升序排列的。如果不对元素进行排序,就会发现它们是按照随机的顺序获取的,而不是按它们的数字顺序进行获取的。

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐