oracle預存程序+遊標處理select資料,oracle預存程序

來源:互聯網
上載者:User

oracle預存程序+遊標處理select資料,oracle預存程序

create or replacePROCEDURE UPDATE_RECORDCODE is  cursor location_data is select * from location where remark in('952701','9527008','952705');--申明遊標  serviceCode NUMBER:=1;BEGIN    for l in location_data loop  --遍曆遊標      BEGIN        --業務處理         UPDATE SERIAL_CODE SET CUR_NUMBER = CUR_NUMBER +1 where AREA_CODE=l.area_code and LOC_TYPE=l.type;         select  CUR_NUMBER into serviceCode from SERIAL_CODE where AREA_CODE=l.area_code and LOC_TYPE=l.type;                  update location lu set putrecord_code=(l.area_code||'0'||l.type||lpad(serviceCode,6,0)) where lu.id=l.id;          --dbms_output.put_line(ServiceCode);         EXCEPTION--異常處理        WHEN NO_DATA_FOUND THEN            INSERT INTO SERIAL_CODE (AREA_CODE, LOC_TYPE,CUR_NUMBER) VALUES (l.area_code,l.type,1);       end;    end loop;     commit;END UPDATE_RECORDCODE;


oracle預存程序效率問題,處理1500w的資料插入

處理大批資料的時候,遊標對逐行處理會很慢,而且你的寫法,commit在迴圈中,也會很慢。
其實你的功能可以用一個不迴圈的語句解決。
假設你定義遊標的語句是(假設你的來源資料表名為test):
select rpt.a,rpt.b,rpt.c,rpt.zy,rpt.zc,rpt.zd,rpt.zr,rpt.df from test rpt;
你的功能可以用以下語句解決:
insert into targ@dc_link (a,b,c,bianma,zhi)
select rpt.a,rpt.b,rpt.c,'0'||num,decode(num,1,rpt.zy,2,rpt.zc,3,rpt.zd,4,rpt.zr,5,rpt.df) from test rpt,(select rownum num from test where rownum<6);
commit;

這樣的好處是語句和commit都只有一個,會快很多。

如果覺得有協助,請採納:)
 
oracle資料庫的預存程序中可以用到隱形遊標但是我不太明白為何可以用 for in loop來完成對資料的處理

loop你打錯了~

打一個比喻來解釋吧

for 100元 in ( 你的銀行卡)
loop
用100元買東西;
錢用完了;
end loop;

意思是每次取出來100元,花沒了再取,一直到你銀行卡裡沒錢為止

所謂顯式遊標,就是聲明過遊標,可以理解為顯示出來讓別人知道;
至於隱式遊標,不需要聲明,直接默默的使用就行了
你這個是for迴圈的隱式遊標
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.