oracle基础总结(十二)
记录表类型
?? 它可以处理多个记录或多个行记录。
1、? 为什么使用记录表类型呢?
?因为我们查询的数据的往往需要返回多行记录,所以需要记录表类型。
?
2、? 定义记录表类型
TYPE table_name is table of data_type[not null]
???? Index by binary_integer;//主键的索引
declare
?? type table_emp is table of emp%rowtype //创建一个表 此表的类型与emp表的类型一致
?? index by binary_integer;
?
?? type table_text is table of varchar2(20) //创建一个表 此表具有一个varchar2列的简单表
?? index by binary_integer;
??
?? empt? table_emp; //声明记录表类型的变量
?? tabtext table_text;
begin
?
?
案例:
SQL> declare
? 2???? type table_emp is table of emp%rowtype
? 3???? index by binary_integer;
? 4???? empt table_emp;
? 5? begin
? 6???? empt(1).ename:='wangyi';
? 7???? dbms_output.put_line(empt(1).ename);
? 8? end;
? 9? /
?
//返回总记录
SQL> declare
? 2???? type table_emp is table of emp%rowtype
? 3???? index by binary_integer;
? 4???? empt table_emp;
? 5? begin
? 6???? dbms_output.put_line(empt.count);
? 7? end;
? 8? /
?
0? //没有记录
?
?
//删除的操作
? 表名.Delete(记录数);
?
//检索记录变量
First:获取第一个的索引
Next:下一个的索引 但是必须有参数
Last:最后一个的索引
SQL> declare
? 2???? type table_emp is table of emp%rowtype
? 3???? index by binary_integer;
? 4???? empt table_emp;
? 5???? i number(2):=1;
? 6? begin
? 7????? while i<10
? 8????? loop
? 9??????? empt(i).ename:='wangyi';
?10??????? i:=i+1;
?11????? end loop;
?12?
?13???? dbms_output.put_line(empt.count);
?14?
?15???? empt.delete(2);
?16?
?17???? dbms_output.put_line(empt.count);
?18?
?19???? dbms_output.put_line(empt.first);
?20???? dbms_output.put_line(empt.next(2));
?21???? dbms_output.put_line(empt.last);
?22? end;
?23? /