oracle update關聯表的思路總結

來源:互聯網
上載者:User

1、
          其中最普通的是update t1 set b=(select b from t2 where t1.a=t2.a);
          但是,要注意空值的影響,
          如果怕空值的影響,要寫成
          update t1 set tname= (select sname from t2 where t1.id=t2.id)
          where exists
          (select 1 from t2 where t1.id=t2.id);

 

          2、對視圖的UPDATE語句

          update (
          select /*+use_hash(t1,t2)*/ t1.tname b1,t2.sname b2
          from t1,t2 where t1.id=t2.id)
          set b1=b2;
         
          這種方法效率高,但是要注意兩個關聯欄位都要有唯一性索引!避免ORA-01427: 單行子查詢返回多個行

 

          3、預存程序
          declare
            cursor c is select t1.*,t1.rowid from t1;
          begin
            for c1 in c
            loop
              update t1 set tname=(select sname from t2 where id=c1.id)
              where rowid=c1.rowid and exists (select 1 from t2 where c1.id=t2.id);
            end loop;
          end;
          但是還是要注意要有exists的語句,否則一樣解決不了空值問題

 

              4、merge也可以進行update:

               
思路4:merge update更新

聯繫我們

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