merge into的用法


merge into 是英文的一個短語,意思是匯入,合並。顧名思義,merge into是合並了insert和update操作,其執行效率要高於分別單獨執行insert和update語句。

//創建表

create table YLB_TEST_001
(
  ID   NUMBER,
  NAME VARCHAR2(20)
);

create table YLB_TEST_002
(
  ID   NUMBER,
  NAME VARCHAR2(20)
);

//插入數據

 

insert into ylb_test_001 values(1,'Arlene');

 

insert into ylb_test_001 values(2,'Bobby');

 

insert into ylb_test_001 values(3,'Tommy');

 

insert into ylb_test_001 values(4,'Jackey');

insert into ylb_test_002 values(1,'Arlene001');

//執行sql

merge into ylb_test_002 a
using ylb_test_001 b
on (a.id=b.id)
when matched then
  update set a.name = b.name
when not matched then
  insert values (b.id,b.name);

結果:

 

小提示:merge into  目標表   using  源表  on (匹配條件) when matched then  執行update 操作 或 delete操作   when not matched then  執行 insert 操作。

           oracle不支持delete操作;

           在SQL2008中,新增了一個關鍵字:Merge,這個和Oracle的Merge的用法差不多,只是新增了一個delete方法而已。

          源表 匹配條件字段unique

      

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM