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;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章