嵌套表集合

--索引表集合

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;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷雨雪白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值