Oracle 建立預存程序

來源:互聯網
上載者:User

create or replace procedure DongjieToTable                             --建立一個名為donjietotable的預存程序
as                                                                                                        --定義資料區
  cursor c_qydjsx                                                                              --定義一個遊標
    is select nbxh from aqydjsx;
  cursor c_sttzqk
    is select tznbxh from asttzqk;

  V_nbxh varchar2(40);                                                                  --定義一個變數
  V_tznbxh varchar2(40);
 
begin                                                                                                --代碼塊開始區
     open c_qydjsx();                                                                       --開啟一個遊標
     fetch c_qydjsx into V_nbxh;   
     while c_qydjsx%found loop
       update aqydjsx set djlx = '01'
       where '1' in
       (select sfjd from qydongjie where qynbxh=V_nbxh and djjdjzsj = (select max(djjdjzsj) from qydongjie where qynbxh=V_nbxh))
       and '0' not in
       (select sfjd from qydongjie where qynbxh=V_nbxh and djjdjzsj = (select max(djjdjzsj) from qydongjie where qynbxh=V_nbxh))
       and nbxh = V_nbxh;         
     fetch c_qydjsx into V_nbxh;
     end loop;
     commit;
     open c_sttzqk();
     fetch c_sttzqk into V_tznbxh;   
     while c_sttzqk%found loop
       update asttzqk set djlx = '01'
       where '1' in
       (select sfjd from qydongjie where qynbxh=V_tznbxh and djjdjzsj = (select max(djjdjzsj) from qydongjie where qynbxh=V_tznbxh))
       and '0' not in
       (select sfjd from qydongjie where qynbxh=V_tznbxh and djjdjzsj = (select max(djjdjzsj) from qydongjie where qynbxh=V_tznbxh))
       and tznbxh = V_tznbxh;          
     fetch c_sttzqk into V_tznbxh;
     end loop;
     commit;
  EXCEPTION                                                                           --異常塊,當以上代碼執行有異常時將執行這裡
  WHEN others THEN
    rollback;
end;                                                                                            --結束代碼塊

 要執行建立好的預存程序在Command中exec dongjietotable;

 

如果在代碼中要根據一個動態值去查一個表,那麼可以用有參遊標

建立方法

cursor c_qynjjbqk(EpNbxh varchar2)                                         --變數名為EpNbxh,類型為varchar2
   is select max(nd) from qynjjbqk where nbxh=EpNbxh;

調用方法

open c_qynjjbqk(nbxh);                                                               --"()"內為要傳入的參數值

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.