for all的使用,批量绑定

  • 在insert语句中使用批量绑定
    • 未使用for all的情况
declare
    type id_table_type is table of number(6) index by binary_integer;
    type name_table_type is table of varchar2(10) index by binary_integer;
    id_table id_table_type;
    name_table name_table_type;
    start_time number(10);
    end_time number(10);
begin
    for i in 1..50000
    loop
        id_table(i) := i;
        name_table(i) := 'Name' || to_char(i);
    end loop;
    start_time := dbms_utility.get_time;
    for i in 1..id_table.count
    loop
        insert into demo values(id_table(i), name_table(i));
    end loop;
    end_time:= dbms_utility.get_time;
    dbms_output.put_line('总计时间(秒)'||to_char((end_time-start_time)/100));
end;

使用for all的情况
declare
    type id_table_type is table of number(6) index by binary_integer;
    type name_table_type is table of varchar2(10) index by binary_integer;
    id_table id_table_type;
    name_table name_table_type;
    start_time number(10);
    end_time number(10);
begin
    for i in 1..50000
    loop
        id_table(i) := i;
        name_table(i) := 'Name' || to_char(i);
    end loop;
    start_time := dbms_utility.get_time;
    forall i in 1..id_table.count

        insert into demo values(id_table(i), name_table(i));

    end_time:= dbms_utility.get_time;
    dbms_output.put_line('总计时间(秒)'||to_char((end_time-start_time)/100));
end;
  • 数据准备
create table demo2(id number(3));
insert into demo2 values(10);
insert into demo2 values(11);
insert into demo2 values(12);
insert into demo2 values(13);
insert into demo2 values(14);
insert into demo2 values(15);

create table demo2_bak as select * from demo2 where 1=2
  • DML之delete
declare  
type id_table_type is table of number(6);  
id_table id_table_type;  
begin  
id_table:=id_table_type(11,null,12,null,13,null);  
forall i in indices of id_table  
    delete from  demo2 where id=id_table(i);  
end;
  • DML之insert
declare  
type id_table_type is table of number(6);  
id_table id_table_type;  
begin  
id_table:=id_table_type(11,null,12,null,13,null);  
forall i in indices of id_table  
    -- dbms_output.put_line('value is :'||id_table(i));
    insert into demo2_bak values(id_table(i));
end;
  • DML之update
declare  
type id_table_type is table of number(6);  
id_table id_table_type;  
begin  
id_table:=id_table_type(11,null,12,null,13,null);  
forall i in indices of id_table  
    -- dbms_output.put_line('value is :'||id_table(i));
    update demo2_bak set id=0 where id=id_table(i);
end;

注意:for all只能用于DML操作,不能用于查询等操作(比如将元素进行显示dbms_output.put_line();)

posted @ 2017-10-27 15:33  岳麓丹枫  阅读(220)  评论(0编辑  收藏  举报