北大青鸟oracle学习笔记31

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类型取代。这里我图个方便。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值