跳到主要内容

集合类型和RECORD类型

GBase 8s 支持三种集合类型:关联数组、可变数组、嵌套表。

关联数据

关联数组是一组键值对。每个键都是一个唯一的索引,用于定位与索引相关联的值。语法

variable_name(index)。

参数说明

index:是字符串类型(VARCHAR2、VARCHAR)。索引按排序顺序存储。

应用样例:

> set environment sqlmode 'oracle';

Environment set.

> set serveroutput on;

set serveroutput succeed.


DECLARE
TYPE population IS TABLE OF NUMBER -- 定义关联数组类型
INDEX BY VARCHAR2(64); -- 用字符类型做索引
city_population population; -- 声明关联数组变量
i VARCHAR2(64);
BEGIN
-- 给数组添加key:values
city_population('Smallville') := 2000;
city_population('Midland') := 750000;
city_population('Megalopolis') := 1000000;
-- 改变key='Smallville'的值
city_population('Smallville') := 2001;
-- 输出关联数组结果
i := city_population.FIRST;
WHILE i IS NOT NULL LOOP
DBMS_Output.PUT_LINE
('Population of ' || i || ' is ' || city_population(i));
i := city_population.NEXT(i);
END LOOP;
END;
/

PL/SQL procedure successfully completed.

Population of Megalopolis is 1000000.0000000000000000
Population of Midland is 750000.0000000000000000
Population of Smallville is 2001.0000000000000000

可变数组

变量(变量大小数组)是一个数组,其元素数量可以从零(空)到声明的最大大小不等。

语法

variable_name(index)

参数说明

index:索引最低边界值是1,上限是当前元素的数量。上限会随着您添加或删除元素而变化,但不能超过最大大小。当您从数据库中存储和检索变量时,其索引和元素顺序保持不变。

应用样例:

DECLARE
TYPE Foursome IS VARRAY(4) OF VARCHAR2(15); -- VARRAY type
-- varray variable initialized with constructor:
team Foursome := Foursome('John', 'Mary', 'Alberto', 'Juanita');
BEGIN
team(3) := 'Pierre'; -- Change values of two elements
DBMS_OUTPUT.PUT_LINE('--res 1--');
FOR i IN 1..4 LOOP
DBMS_OUTPUT.PUT_LINE(i || '.' || team(i));
END LOOP;
DBMS_OUTPUT.PUT_LINE('--res 1--');
team(4) := 'Yvonne';
DBMS_OUTPUT.PUT_LINE('--res 2--');
FOR i IN 1..4 LOOP
DBMS_OUTPUT.PUT_LINE(i || '.' || team(i));
END LOOP;
DBMS_OUTPUT.PUT_LINE('--res 2--');
team := Foursome('Arun', 'Amitha', 'Allan', 'Mae');
DBMS_OUTPUT.PUT_LINE('--res 3--');
FOR i IN 1..4 LOOP
DBMS_OUTPUT.PUT_LINE(i || '.' || team(i));
END LOOP;
DBMS_OUTPUT.PUT_LINE('--res 3--');
END;
/

PL/SQL procedure successfully completed.

--res 1--
1.John
2.Mary
3.Pierre
4.Juanita
--res 1--
--res 2--
1.John
2.Mary
3.Pierre
4.Yvonne
--res 2--
--res 3--
1.Arun
2.Amitha
3.Allan
4.Mae
--res 3--

嵌套表

嵌套表是一种列类型,它以不特定顺序存储未指定数量的行。

语法

variable_name(index)

参数说明

index:从数据库中存储和检索嵌套表时,嵌套表的索引和行顺序可能不会固定。

限制
  • 不支持多行结果bulk collection into语句赋值,如例1。

  • 支持单行结果对嵌套表列的into variable用法赋值,如例2。

  • 支持单行结果对嵌套表列的into record用法赋值,但需要单独指定字段,不能用table%rowtype,如例3。

  • 不支持 the(subquery)语法.

应用样例

> create type tp1 is table of int;
> /

Oracle Type created.

> create table t1(c1 int,c2 tp1) nested table c2 store as nest_c2;

Table created

插入1条数据。
insert into t1 values(1,tp1(1,2,3,4));

1 row(s) inserted.

例1:不支持多行结果bulk collection into语句赋值
declare
type tp2 is table of tp1;
ini_tp2 tp2;
begin
select c2 bulk collect into ini_tp2 from t1;
dbms_output.put_line(ini_tp2(1)(1));
end;

201: A syntax error has occurred.

例2:支持单行结果对嵌套表列的into variable用法赋值
declare
ini_tp2 tp1;
begin
select c2 into ini_tp2 from t1 where rownum=1;
dbms_output.put_line(ini_tp2(1));
end;

PL/SQL procedure successfully completed.

1

例3:支持单行结果对嵌套表列的into record用法赋值
declare
type re is record(c1 int,c2 tp1);
ini_tp2 re;
begin
select * into ini_tp2 from t1 where rownum=1;
dbms_output.put_line(ini_tp2.c2(1));
end;
PL/SQL procedure successfully completed.

1