1. 程式人生 > >merge into 插入或更新資料

merge into 插入或更新資料

1.Oracle中Merge into能整合update和insert兩個語句

2.這個語法僅需要一次全表掃描就完成了全部工作,執行效率要高於INSERT+UPDATE

語法:

MERGE [INTO] [schema.]table [alias]
USING {[schema.]table|views|query} [alias]
ON {condition}
WHEN MATCHED THEN UPDATE SET {clause}
WHEN NOT MATCHED THEN INSERT VALUES {clause}

例子:

 

重複資料不插入+不重複則插入 : 
merge into haode h using(select '文迪1' as name from dual) b on(h.name=b.name) when not matched then insert values(7,'文迪1','好的6');


-->0行合併...

重複資料更新+不重複則插入:

merge into haode h using(select '文迪1' as name,'好的merge' as character from dual) b on(h.name=b.name) when matched then update set h.character=b.character when not matched then insert values(7,'文迪7','merge');

-->1行合併...

參考http://www.cnblogs.com/jiangguang/archive/2012/12/24/2830581.html