oracle 存储过程循环执行update语句

本文提供了一个使用存储过程批量更新数据库表中特定列的示例。通过创建一个存储过程,利用bulk collect特性来提高效率,并展示了如何遍历收集的数据并对每一项进行更新。



select  column1  from table1 ;对column1进行update操作。由于update时,需要对column1进行特殊处理。如果是java程序里,可以先for循环,然后分别update。请问在存储过程里如何实现?
请写一个较为完成的存储过程。我会适当再加分的。谢谢!
2012-06-28 13:43 提问者采纳其实二楼写的最简单,但对于新手,最好别那么写,至于1楼,如果数据不是很多,没必要搞个游标。你也可以看看我写的
create or replace procedure P_Update(o_vc_message out  varchar2)
is 
type column1 is table of table1.column1%type index by binary_integer;
col1s column1;
type rid is table of rowid index by binary_integer;
rids rid;
temp table1.column1%type;
begin
  select column1,rowid bulk collect into col1s,rids from table1;
  if (column1.count != 0) then
    for  i in col1s.first..col1s.last loop
      temp := col1s(i);--处理 col1s(i) 想干嘛干嘛
      update table1  set column1 = temp where rowid = rids(i);
    end loop;
  end if; 
  o_vc_message := 'OK!';
exception 
  when others then
    o_vc_message := 'exception happend.' || sqlcode || sqlerrm;
    rollback;
    return;
end P_Update; 

如果仅仅是简单处理column1,比如加1什么的,就别搞那么复杂,一个sql就ok了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值