?
Merge是一個很實用的功能,相似于Mysql里的insert into on duplicate key.?
Oracle在9i引入了merge命令,?
通過這個merge你可以在一個SQL語句中對一個表同一時候運行inserts和updates操作. 當然是update還是insert是根據于你的指定的條件推斷的,Merge into可以實現用B表來更新A表數據,假設A表中沒有,則把B表的數據插入A表. MERGE命令從一個或多個數據源中選擇行來updating或inserting到一個或多個表?
語法例如以下?
MERGE INTO [your table-name] [rename your table here]?
USING ( [write your query here] )[rename your query-sql and using just like a table]?
ON ([conditional expression here] AND [...]...)?
WHEN MATHED THEN [here you can execute some update sql or something else ]?
WHEN NOT MATHED THEN [execute something else here ! ]?
我們先看看一個簡單的樣例,來介紹一個merge into的使用方法?
merge into products p using newproducts np on (p.product_id = np.product_id)?
when matched then?
update set p.product_name = np.product_name?
when not matched then?
insert values(np.product_id, np.product_name, np.category)?
在這個樣例里。前面的merger into products using newproducts 表示的用newproducts表來merge到products表,merge的匹配關系就是on后面的條件子句的內容,這里依據兩個表的product_id來進行匹配,那么匹配上了我們的操作是就是when matched then的子句里的動作了,這里的動作是update set p.product_name = np.product_name, 非常顯然就是把newproduct里的內容,賦值到product的product_name里。假設沒有匹配上則insert這種一條語句進去。 大家看看這個merget inot的使用方法是不是一目了然了呀。這里merger的功能,好比比較,然后選擇更新或者是插入,是一系列的組合拳,在做merge的時候,這樣相同的情況下,merge的性能是優于同等功能的update/insert語句的。有人以前分析merge是批量處理對性能貢獻非常大,個人認為這個是沒有考據的。?
我們也能夠在using后面使用視圖或者子查詢。比方我們把newproducts換成?
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)?
when matched then?
update set p.product_name = np.product_name?
when not matched then?
insert values(np.product_id, np.product_name, np.category)?
也是能夠的。?
在Oracle 10g中MERGE有例如以下一些改進:?
1、UPDATE或INSERT子句是可選的?
2、UPDATE和INSERT子句能夠加WHERE子句?
3、在ON條件中使用常量過濾謂詞來insert全部的行到目標表中,不須要連接源表和目標表?
4、UPDATE子句后面能夠跟DELETE子句來去除一些不須要的行?
我們通過實例來一一看看如上的新特性?
1. UPDATE或INSERT子句是可選的?
在9i里因為必須insert into和update都要存在,也就是不是update就是insert,不支持單一的操作,盡管還是能夠曲線救國,呵呵 可是有些過于強勢了。而10g里就是可選了,能符合我們很多其它的需求了?
比方上面的句子?
我們能夠僅僅存在update或者insert?
merge into products p using newproducts np on (p.product_id = np.product_id)?
when matched then?
update set p.product_name = np.product_name?
這里,假設匹配就更新,不存在就無論了。?
2. UPDATE和INSERT子句能夠加WHERE子句?
這也是一個功能性的改進,可以符合我們很多其它的需求,這個where的作用非常明顯是一個過濾的條件,是我們增加一些額外的條件,對僅僅對滿足where條件的進行更新和insert?
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)?
when matched then?
update set p.product_name = np.product_name where np.product_name like 'OL%'?
這里表示僅僅是對product_name開頭是'OL'的匹配上的進行update,假設開頭不是'OL'的就是匹配了也不做什么事情,insert里也能夠增加where?
比方?
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)?
when matched then?
update set p.product_name = np.product_name where np.product_name like 'OL%'?
when not matched then?
insert values(np.product_id, np.product_name, np.category) where np.product_name like 'OL%'?
這里注意比較一下,他們返回的結果行數,是有著差異的。?
3. 在ON條件中使用常量過濾謂詞來insert全部的行到目標表中,不須要連接源表和目標表?
merge into products p using (select * from newproducts) np on (1=0)?
when matched then?
update set p.product_name = np.product_name?
when not matched then?
insert values(np.product_id, np.product_name, np.category)?
個人認為這個功能沒有太大的意義,我們的insert into本身就支持這種功能,沒有必要使用merge?
4. UPDATE子句后面能夠跟DELETE子句來去除一些不須要的行?
delete僅僅能和update配合,從而達到刪除滿足where條件的子句的紀錄?
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)?
when matched then?
update set p.product_name = np.product_name delete where p.product_id = np.product_id where np.product_name like 'OL%'?
when not matched then?
insert values(np.product_id, np.product_name, np.category)?
這里我們達到的目的就是 會把匹配的記錄的prodcut_name更新到product里,而且把product_name開頭為OL的刪除掉。
merge into也是一個dml語句,和其它的dml語句一樣須要通過rollback和commit 結束事務。?
Merge是一個很強大的功能,并且是我們需求里常常會用到的一個實用的功能,所以我們一定要好好的學習到。?
文中須要的測試腳本在附件里提供下載。?
merge into sample.sql
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
