您的位置:首页 > 数据库 > Oracle

Oracle批量逐条数据处理

2017-05-09 17:00 197 查看
----批量逐条数据处理模版-----

create or replace procedure proc_test is

   batch_size constant pls_integer := 1000;--批量处理数量

--定义结果集数组

type data_result is record(

    tn_column1 tablename.column1%type,

    tn_column2 tablename.column4%%type,

    tn_column3 varchar2(4000));

  type data_result is varray(1000) of data_result;--数组大小

  results data_result;

--定义游标

  cursor cur_curname is

    select column1, column2, column3, column14

      from (select t.column1,

                   t.column2,

                   t.column3,

                   wm_concat(t.column4) column4 --列转行

              from tablename t

             group by t.column1, t.column2, t.column3)

     where length(column4) > 2;

begin

  open cur_curname; --打开游标

  loop

    fetch cur_curname bulk collect

      into results limit batch_size;

    for i in 1 .. results.count loop --逐条处理

      --业务逻辑处理

      dbms_output.put_line(results.count);

    end loop;

    exit when cur_curname%notfound;
  end loop;

--关闭游标

  if cur_curname%isopen then

    close cur_curname;

  end if;

  commit;

exception

  when others then

    dbms_output.put_line('error : ' || sqlcode || '--' || sqlerrm);

    rollback;

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息