天天看點

oracle 存儲過程循環執行update語句

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了。      

繼續閱讀