語法:(其中as可以省略)
merge into table_name as table_alias
using (table|view|sub_query) as alias
on (join condition)
when matched then
update set
col1 = col_val1,
col2 = col2_val --9i 不可以有where條件,10g 可以
when not matched then
insert (column_list)—多個列以逗號分割 //可以不指定列
values (column_values); --9i 不可以有where條件,10g 可以
作用:將源資料(**於實際的表,檢視,子查詢)更新或插入到指定的表中(必須實際存在),依賴於on條件,好處是避免了多個insert 和update操作。merge是乙個目標性明確的操作符,不允許在乙個merge 語句中對相同的行insert 或update 操作。這個語法僅需要一次全表掃瞄就完成了全部工作,執行效率要高於insert+update。例子如下:
drop table t;
create table t as select rownum id, a.* from dba_objects a;
drop table t1;
create table t1 as
select rownum id, owner, table_name, cast('table' as varchar2(100)) object_type
from dba_tables;
select * from dba_objects;
select * from dba_tables;
merge into t1 using t
on (t.owner = t1.owner and t.object_name = t1.table_name and t.object_type = t1.object_type)
when matched then update set t1.id = t.id
when not matched then insert values (t.id, t.owner, t.object_name, t.object_type);--insert
後面不寫表示插入全部列
merge into t1 using t
on (t.owner = t1.owner and t.object_name = t1.table_name)
when matched then update set t1.id = t.id
when not matched then insert values (t.id, t.owner, t.object_name, t.object_type);--常見錯誤,
連線條件不能獲得穩定的行,可以使用下面的用子查詢
merge into t1
using (select owner, object_name, max(id) id from t group by owner, object_name) t
on (t.owner = t1.owner and t.object_name = t1.table_name)
when matched then update set t1.id = t.id
when not matched then insert values (t.id, t.owner, t.object_name);
select id, owner, object_name, object_type from t
minus
select * from t1;
drop table subs;
create table subs(msid number(9),
ms_type char(1),
areacode number(3)
);drop table acct;
create table acct(msid number(9),
bill_month number(6),
areacode number(3),
fee number(8,2) default 0.00);
insert into subs values(905310001,0,531);
insert into subs values(905320001,1,532);
insert into subs values(905330001,2,533);
commit;
merge into acct a --操作的表
using subs b on (a.msid=b.msid)--使用原始資料**的表,並且制定條件,條件必須有括號
when matched then
update set a.areacode=b.areacode--當匹配的時候,執行update操作,和直接update的語法
不一樣,不需要制定表名
when not matched then--當不匹配的時候,執行insert操作,也不需要制定表名,若指定字段插入,則在insert後用括號標明,不指定是全部插入
insert(msid,bill_month,areacode) values(b.msid,'200702',b.areacode);
另外,merge語句的update不能修改用於連線的列,否則會報錯
select * from acct;
select * from subs;
--10g新特性,單個操作
merge into acct a
using subs b on(a.msid=b.msid)
when not matched then--只有單個not matched的時候,只做插入,不做更新,只有單個matched的時候,只做更新操作
insert(a.msid,a.bill_month,a.areacode) values(b.msid,'200702',b.areacode);
update acct set areacode=800 where msid=905320001;
delete from acct where areacode=533 or areacode=531;
insert into acct values(905320001,'200702',800,0.00);
--刪除重複行
delete from subs b where b.rowid<(
select max(a.rowid) from subs a where a.msid=b.msid and a.ms_type=b.ms_type and
a.areacode=b.areacode);
--10g新特性,merge操作之後,只有匹配的update操作才可以,用delete where子句刪除目標表中滿足條件的行。
merge into acct a
using subs b on (a.msid=b.msid)
when matched then
update set a.areacode=b.areacode
delete where (b.ms_type!=0)
when not matched then
insert(msid,bill_month,areacode)
values(b.msid,'200702',b.areacode)
where b.ms_type=0;
--10g新特性,滿足條件的插入和更新
merge into acct a
using subs b on (a.msid=b.msid)
when matched then
update set a.areacode=b.areacode
where b.ms_type=0
when not matched then
insert(msid,bill_month,areacode)
values(b.msid,'200702',b.areacode)
where b.ms_type=0;
select * from subs where ms_type=0;
MERGE INTO比對不同資料
create table table a id varchar 20 name varchar 20 varchar 20 age varchar 20 loaddate varchar 20 create table table b id varchar 20 name varchar 20 va...
Merge into 詳細介紹
merge語句是oracle9i新增的語法,用來合併update和insert語句。通過merge語句,根據一張表或子查詢的連線條件對另外一張表進行查詢,連線條件匹配上的進行update,無法匹配的執行insert。這個語法僅需要一次全表掃瞄就完成了全部工作,執行效率要高於insert update...
使用merge into總結
第二次使用merge into時依舊出現了問題,覺得這個語句即是常用語句並且效率也高,所以總結下 1.用於對比的字段不能放在update下面,比如id之類的 2.string truncted。報出這個異常是由於某些欄位在做更新的過程中,目標表的字段長度小於匯入值,需要修改目標表字段的長度。3.由於...