pl/sql 表
在pl/sql块中临时使用、像数组一样的对象
包含一列和一个主键
不能对列和主键进行命名
列可以是任何标量数据类型
主键必须是binary_integer类型
大小没有限制
声明pl/sql表
定义表的类型
type 类型名 is table of 列类型|变量数据类型 index by binary_integer;
声明表变量
表名 类型名;
引用pl/sql表
表名(下标)
赋值
表名(下标):=表达式;
属性方法:
count --返回pl/sql表的总行数;
delect --删除pl/sql表的所有内容;
delect(行数) --删除pl/sql表的指定的行;
delct(开始行,结束行) --删除pl/sql表的多行;
first --返回表的第一个INDEX;
next(行数) --这个行数的下一条的INDEX;
last --返回表的最后一个INDEX;
declare Type stuNameTableType is table of student.stu_name%type index by binary_integer; stuNameTable stuNameTableType; begin for i in 1..10 loop stunametable(i):='student'||i; end loop; for i in 1..stunametable.count loop dbms_output.put_line('第'||i||'个元素:'||stunametable(i)); end loop; end;
表类型需要使用bulk collect批量绑定进行select赋值,bulk collect子句也可用于Fetch bulk collect into 子句中
declare TYPE studentTableType is table of student%rowtype index by binary_integer; studentTable studentTableType; begin select * bulk collect into studentTable from student order by stu_id; for i in 1..studentTable.count loop dbms_output.put_line(studentTable(i).stu_id); dbms_output.put_line(studentTable(i).stu_name); dbms_output.put_line(studentTable(i).stu_sex); dbms_output.put_line(studentTable(i).stu_birthday); end loop; end;
Record类型
声明
type 记录类型名 is record (
字段1 类型[not null[:=表达式]]
字段n 类型[not null[:=表达式]]
)
not null 列必须在声明部分初始化,记录类型可以嵌套。
变量 记录类型;
赋值:变量名.列名:=表达式:
declare type studentRecord is RECORD( sid student.stu_id%type, sname student.stu_name%type, ssex student.stu_sex%type ); stuRec studentRecord; begin select stu_id,stu_name,stu_sex into sturec from student where stu_id = 1; dbms_output.put_line(sturec.sid); dbms_output.put_line(sturec.sname); dbms_output.put_line(sturec.ssex); end;
pl/sql表和record类型可以结合使用
declare type studentRecord is RECORD( sid student.stu_id%type, sname student.stu_name%type, ssex student.stu_sex%type, sbirthday student.stu_birthday%type ); TYPE studentTableType is table of studentRecord index by binary_integer; studentTable studentTableType; begin select * bulk collect into studentTable from student order by stu_id; for i in 1..studentTable.count loop dbms_output.put_line(studentTable(i).sid); dbms_output.put_line(studentTable(i).sname); dbms_output.put_line(studentTable(i).ssex); dbms_output.put_line(studentTable(i).sbirthday); end loop; end;
record和pl/sql表的结合感觉类似于java中泛型数组的概念,有效结合应该还是满实用的,上面那个例子实用性实在有点鸡肋,完全可以被rowtype类型取代。这里我图个方便。。。
本文详细介绍了在PL/SQL块中如何使用PL/SQL表与Record类型进行数据操作,包括声明、引用、属性方法及结合使用示例。
159

被折叠的 条评论
为什么被折叠?



