Oracle多表串聯更新詳解

來源:互聯網
上載者:User

用遊標實現,我覺得絕對這種方法比較安全的。
--首先定一個遊標把需要用到的一些資料存放到遊標中:

複製代碼 代碼如下:declare
  CURSOR D_CURSOR_CUS_INFO IS
    select t3.id_           as id_,
           t3.owe_money_    as owe_money_,
           a.heatingArea    as heating_area_
    from T_CUS_OWE_MONEY_2 t2
    left join T_CUS_OWE_MONEY_3 t3 on t2.id_= t3.id_
    left join (select s.bh,  sum(
           case
                 when s.stkbz='0' then nvl(s.mj,0)
                 when s.stkbz='1' then 0-nvl(s.mj,0)
           end
      ) as heatingArea from  sk s  where s.nd = '2008-2009' group by s.bh) a on t2.bh_=a.bh
  where  t3.owe_money_- t2.owe_money_  = a.heatingArea*5 and t3.OWE_MONEY_ > 0;

--然後迴圈遊標對資料進行更新:
複製代碼 代碼如下:begin
        FOR everyRow IN D_CURSOR_CUS_INFO
        loop
         update T_CUS_YEAR_STATUS t
             set t.HEATING_AREA_ = everyRow.HEATING_AREA_,
                 t.OWE_MONEY_    = everyRow.owe_money_
             where t.YEAR_ = '2008-2009'
                   and t.id_ = everyRow.id_;
       end loop;
      commit;
end;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.