merge into的妙用,mergeinto妙用

來源:互聯網
上載者:User

merge into的妙用,mergeinto妙用

   今天遇到一個奇葩的需求:系統A是主系統,終端PDA會上傳資料到系統A。當有單據id在系統A的id能查到,則update狀態;當單據id在系統A中查不到則insert。下面來做一個簡單的測試。

drop table test;
create table test(id number primary key, name varchar2(10),state number(1));
insert into test values(1,'單據1',1);
insert into test values(2,'單據2',1);
insert into test values(3,'單據3',1);
commit;

1.當一看到這個需求,我就想到用merge into,於是寫出下列SQL:
merge into test t1
using (select id from test where name = ?) t2
on(t1.id = t2.id)
when matched then
  update set state = ?
when not matched then
  insert (id, name, state) values (?, ?, ?);
這些寫的問題,當select id from test where name = ?有值可以update成功,當沒有值不會insert。

2. 改進一下
merge into test t1
using (select id from test where name = '單據1'
 union select -1 from dual) t2
on(t1.id = t2.id)
when matched then
  update set state = 2
when not matched then
  insert (id, name, state) values (1, '單據1', 1);
這些寫的也有問題,當select id from test where name = ?,select id from test where name = '單據1'不能查出來資料的時候
是ok的,當能查出來資料的時候會報主鍵衝突
merge into test t1
*
第 1 行出現錯誤:
ORA-00001: 違反唯一約束條件 (SYS_C0065462)

3.改進一下,保證using裡面的內容查出一條記錄,不管是否有值
select * from test;
       ID NAME            STATE
---------- ---------- ----------
         1 單據1               1
         2 單據2               1
         3 單據3               1
--PDA的單據在系統A中有
merge into test t1
using (select id from test where name = '單據1'
       union all
       select 1 id
         from dual
        where not exists (select id from test where name = '單據1')) t2
on (t1.id = t2.id)
when matched then
  update set state = 2
when not matched then
  insert (id, name, state) values (t2.id, '單據1', 1);
--PDA的單據在系統A中沒有
merge into test t1
using (select id from test where name = '單據10'
       union all
       select 10 id
         from dual
        where not exists (select id from test where name = '單據10')) t2
on (t1.id = t2.id)
when matched then
  update set state = 2
when not matched then
  insert (id, name, state) values (t2.id, '單據1', 1);
  
 select * from test;
    ID NAME            STATE
--------- ---------- ----------
         1 單據1               2
         2 單據2               1
         3 單據3               1
        10 單據1               1

聯繫我們

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