用存储过程批量更新表

2015-02-02 20:09:16 · 作者: · 浏览: 18

最近做了一个需求,需要批量更新数据库表,但是因为涉及到的数据较多(千万条),如果直接用sql更新,估计会把pl/sql弄垮

sql如下:update online_product set online_flag = '0' where status = 'ON'

所以,写了一个存储过程,以备忘:

declare
  cursor product_id_list is
    select product_id
      from online_product
     where status = 'ON';
  commit_count number := 0;
  total_count number := 0;
begin
  for pid in product_id_list loop
    total_count := total_count + 1;
    commit_count := commit_count + 1;
    update online_product
       set online_flag = '0'
     where status = 'ON'
       and product_id = pid.product_id;
    if commit_count > 1000 then
      commit;
      commit_count := 0;
    end if;
  end loop;
  commit;
  dbms_output.put_line('Total Count:' || total_count);
end;