LightDB支持存储过程,除了支持Postgres的plpgsql存储过程,还兼容Oracle的存储过程,新增了plorasql过程语言。上一篇中我们介绍了存储过程中的记录类型,这一篇主要讲述存储过程中的集合类型。
前面我们讲过,PL/SQL支持两种复合数据类型:集合类型和记录类型。他们的区别是:集合类型中所有内部组件具有相同的数据类型,而记录类型内部组件可以具有不同的数据类型。关于记录类型,可参考文章LightDB中的存储过程(五),本文重点讲述集合类型。
集合类型类似于其他高级语言中的数组,是相同类型元素的集合。PL/SQL中的集合类型分为联合数组类型(Associative Array)、嵌套表类型(Nested Table)和可变数组类型(Variable-Size Array)三种。
联合数组,又称索引表,是键值对的集合,每个键是唯一的索引值,没有固定的上下限,即元素的个数是不受限制的。而且其索引值可以是无序的。索引值类型可以是字符类型也可以是数字类型。举个例子:
create or replace procedure shenlan
as
declare
type mytype is table of int index by integer; -- 在存储过程中声明一个联合数组类型
v mytype;
begin
select id bulk collect into v from mystudent where rownum < 5;
raise notice '%', v(1);
end;
/
drop procedure shenlan; -- 同时会删掉类型mytype
联合数组可使用下标访问,举个例子:
create or replace procedure shenlan
as
declare
type mytype is table of int index by integer; -- 在存储过程中声明一个联合数组类型
v mytype;
begin
select id bulk collect into v from mystudent where rownum < 5;
for i in 1..4 loop
raise notice '%', v(i); -- 通过下标访问联合数组
end loop;
end;
/
嵌套表和可变数组,目前暂不支持,后续版本会支持。
更多请参考LightDB官网