PLSQL总结——21.批处理

/*
create table student
(
  id number,
  name varchar2(20)
);
*/

declare

  type student_t is record(
    id   number,
    name varchar2(20)); --student结构体
  type student_nt is table of student_t index by pls_integer; --student索引表

  v_student_t   student_t; --student结构体变量
  v_student_nt  student_nt; --student索引表变量,插入用
  v_student_nt2 student_nt; --student索引表变量,隐式提取
  v_student_nt3 student_nt; --student索引表变量,显示提取

  /*输出studnet数组*/
  procedure show_array(x            number,
                       y            number,
                       p_student_nt student_nt) is
  
  begin
    for i in x .. y
    loop
      dbms_output.put_line(p_student_nt(i)
                           .id || ',' || p_student_nt(i).name);
    end loop;
  end show_array;

begin
  v_student_t.id := 1;
  v_student_t.name := 'a';
  v_student_nt(1) := v_student_t;

  v_student_t.id := 2;
  v_student_t.name := 'b';
  v_student_nt(2) := v_student_t;

  v_student_t.id := 3;
  v_student_t.name := 'c';
  v_student_nt(3) := v_student_t;

  v_student_t.id := 4;
  v_student_t.name := 'd';
  v_student_nt(4) := v_student_t;

  /*forall方式1,数组是要紧凑的*/
  forall rec in v_student_nt.first .. v_student_nt.last
    insert into student values v_student_nt (rec);
  /*forall方式2,indices of,数组不需要紧凑的*/
  /*  
      forall rec in indices of v_student_nt between 1 and 4
      insert into student values v_student_nt (rec);
  */

  /*隐式bulk collect*/
  select * bulk collect into v_student_nt2 from student;
  /*输出v_student_nt2*/
  show_array(v_student_nt2.first, v_student_nt2.last, v_student_nt2);

  dbms_output.put_line('-------------------------------');

  declare
    cursor c1 is
      select * from student;
  begin
    open c1;
    loop
      fetch c1 bulk collect
        into v_student_nt3;
      exit when c1%notfound;
    end loop;
    close c1;
  end;
  /*输出v_student_nt3*/
  show_array(v_student_nt2.first, v_student_nt2.last, v_student_nt3);

end;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值