嵌套表
1用于处理PL/SQL数组的数据类型
2嵌套表的元素下标从1开始
3嵌套表元素个数没有限制
4嵌套表元素的数组元素可以无序
5索引表类型不能作为表列的数据类型,但是嵌套表可以
6使用嵌套表的时候必须初始化,初始化决定下标的大小
语法
type type_name is table of element_type;
indetifer type_name ;
type_name :嵌套表的类型名
element_type:嵌套表元素的数据类型
indetifer :定义嵌套表的变量
declare
type sname_table_type is table of student.sname%type;
sname_table sname_table_type;
begin
sname_table:=sname_table_type(‘lia’,’lib’,’lic’);
select sname into sname_table(1) from student where sno=’s001’;
select sname into sname_table(2) from student where sno=’s002’;
dbms_output.put_line(sname_table(1)||’———’||sname_table(2));
end;
解释
sname_table:=sname_table_type(‘lia’,’lib’,’lic’);
对嵌套表进行初始化.
比较图
没有初始化情况
嵌套表作为表列的数据类型
create type sname_type is table of varchar2(20);
create table student2(
sno varchar2(10) primary key,
sname sname_type,
sage number(2),
cno varchar2(2)
)
nested table sname store as sname_table–为字段sname指定存储表sname_table
;
插入数据
begin
insert into student2 values(‘5’,sname_type(‘lilia’,’lilib’),21,’1’);
end;
显示
declare
sname_table sname_type;
begin
select sname into sname_table from zhou.student2 where sno=’5’;
for i in 1..sname_table.COUNT
loop
dbms_output.put_line(sname_table(i));
end loop;
end;