--索引表集合
declare
type 集合类型名 is table of 元素类型 index by 下标类型;
变量名集合类型名
begin
集合类型名(唯一下标):=元素;
end;
--嵌套表集合
declare
type 嵌套类型名 is table of 元素类型名;
--无参初始化
变量名 嵌套类型名:=嵌套类型名();
--有参初始化
变量名 嵌套类型名:=嵌套类型名(参数1,参数2......);
--空间必须从1到n,必须连接,有几个参数就会开几个空间
begin
--开空间
嵌套变量名.extend; --写一次开一个空间
集合类型名(唯一下标):=元素;
end;
例:无参初始化
declare
type t_a is table of number;
--无参初始化
t_jihe t_a:=t_a(); --最常见
begin
--开空间 --空间个数要大于等于下标人数
(--)t_jihe .extend; --写一次开一个空间 --第一次开空间时,默认从1开始
t_jihe (1) :=111;
t_jihe (2) :=222;
t_jihe (3) :=333;
--t_jihe _extend (3,n); --第二个参数,代表开的空间和元素与下标为n的元素相同,n的大小不能超过已开空间的大小
--t_jihe(7):=444;
--t_jihe(5):=555;
dbms_output.put_line(t_jihe(1));
dbms_output.put_line(t_jihe(2));
dbms_output.put_line(t_jihe(3));
dbms_output.put_line(t_jihe(4));
dbms_output.put_line(t_jihe(5)); --有空间,没有下标跟赋值时,也会占一行
dbms_output.put_line(t_jihe(6));
dbms_output.put_line(t_jihe(7));
end;
例:有参初始化
declare
type t_a is table of number;
--有参初始化
t_jihe t_a:=t_a(777,888,999);
--自动开空间赋值,并且按照从1~n的顺序给下标
begin
t_jihe .extend(4);
t_jihe(1):=111;
--集合类型名与下标相同时,下面的元素会替代上面的元素
t_jihe(5):=222;
t_jihe(6):=333;
dbms_output.put_line(t_jihe(1));
dbms_output.put_line(t_jihe(2));
dbms_output.put_line(t_jihe(3));
dbms_output.put_line(t_jihe(4));
dbms_output.put_line(t_jihe(5));
dbms_output.put_line(t_jihe(6));
dbms_output.put_line(t_jihe(7));
end;
--delete---trim---
declare
type t_a is table of number;
--有参初始化
t_jihe t_a :=t_a(777,888,999);
t_jihe.extend(20);
t_jihe(1):=111; --集合类型名与下标相同时,下面的元素会替换上面的元素
t_jihe(5):=222;
t_jihe(6):=333;
--t_jihe.delete; --删除所有空间+下标+元素
t_jihe.trim; --删除最后一个下标+空间+元素
--t_trim.trim(n);--删除最后一个下标+空间+元素
t_jihe.trim; --删除最后一个下标+空间+元素
--t_jihe.trim(n);--删除最后n个下标+空间+元素
dbms_output.put_line(t_jihe(1));
dbms_output.put_line(t_jihe(2));
dbms_output.put_line(t_jihe(3));
dbms_output.put_line(t_jihe(4));
dbms_output.put_line(t_jihe(5));
dbms_output.put_line(t_jihe(6));
dbms_output.put_line(t_jihe(7));
end;
--遍历empa表种数据
declare
type t_a is table of empa%rowtype;
--无参初始化
t_jihe t_a:=t_a();--自动开空间赋值,并且按照从1~n的顺序给下标
a number:=1;
begin
for n in (select * from empa) loop
t_jihe.exted; --每循环一次开一个空间
t_jihe(a) :=n;--每循环一次下标增长1
a:=a+1;
end loop;
for i in t_jihe.first..t_jihe.last loop
dbms_output.put_line(t_jihe(i)).ename);
end loop;
end;
--自定义类型遍历表中数据
declare
type t_leixing is record (xm varchar2(20),x2 number);
type t_a is table of t_leixing;
--无参初始化
t_jihe t_A:=t_a(); --自动开空间赋值,并且按照从1~n的顺序给下标
a number:=1;
begin
for n in (select * feom empa) loop
t_jihe .extend; --每循环一次下标增长1
end loop;
for i in t_jihe.first.. t_jihe last loop
dbms_output.put_line(t_jihe(i).xm||t_jihe(i).xz);
end loop;
end;