1、MERGE INTO 的用途:
MERGE INTO 是Oracle 9i新增的語(yǔ)法,在10g時(shí)得到補(bǔ)充,用來(lái)合并UPDATE和INSERT語(yǔ)句,根據(jù)一張表或子查詢(xún)與另一張表進(jìn)行連接查詢(xún),連接條件匹配就進(jìn)行
創(chuàng)新互聯(lián)建站專(zhuān)注于徽縣企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開(kāi)發(fā),商城網(wǎng)站制作。徽縣網(wǎng)站建設(shè)公司,為徽縣等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站策劃,專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
UPDATE,不匹配就進(jìn)行INSERT,這個(gè)語(yǔ)法僅需要一次全表掃描就可以完成全部工作,執(zhí)行效率會(huì)比單純的UPDATE+INSERT高,具體應(yīng)用可用于表之間的同步。
2、MERGE INTO 的語(yǔ)法:
語(yǔ)法結(jié)構(gòu):
MERGE [INTO [schema .] table [t_alias]
USING [schema .] { table | view | subquery } [t_alias]
ON ( condition )
WHEN MATCHED THEN merge_update_clause
WHEN NOT MATCHED THEN merge_insert_clause;
語(yǔ)法說(shuō)明:
MERGE INTO [表名] [別名] --需要更新的目標(biāo)表
USING ( 子查詢(xún)/表名/視圖)[別名] --源表
ON ([連接條件] AND [...]...) --連接條件/更新條件
WHEN MATHED THEN UPDATE SET [...] --如果匹配,更新表記錄,若只作更新出來(lái),下面的INSERT部分可以去掉
WHEN NOT MATHED THEN INSERT VALUES() [...] --如果不匹配,插入表記錄
3、MERGE INTO 演示:
1> 創(chuàng)建測(cè)試表及數(shù)據(jù):
--以表YAG1作為源表,表YAG2作為更新的目標(biāo)表
CREATE TABLE YAG1 AS SELECT OBJECT_NAME,oOBJECT_ID FROM USER_OBJECTS WHERE ROWNUM<=10;
CREATE TABLE YAG2 AS SELECT OBJECT_NAME,oOBJECT_ID FROM USER_OBJECTS WHERE ROWNUM<=5;
--修改表YAG1中某條記錄的OBJECT_NAME,創(chuàng)造符合UPDATE的條件,
SQL> UPDATE YAG1 SET OBJECT_NAME='AAAAA' WHERE OBJECT_NAME='T_CAT';
2>MERGE INTO 更新前兩表的記錄對(duì)比:
SQL> SELECT A.OBJECT_ID,A.OBJECT_NAME,B.OBJECT_NAME FROM YAG1 A,YAG2 B WHERE A.OBJECT_ID=B.OBJECT_ID(+) ORDER BY 1;
A.OBJECT_ID A.OBJECT_NAME B.OBJECT_NAME
------------ ---------------- -----------------
46366 AAAAA T_CAT
46367 SUM_STRING SUM_STRING
46368 ARRAYLIST ARRAYLIST
46369 TYSKZ_SJDX TYSKZ_SJDX
46370 TYSKZ_SJXMGX TYSKZ_SJXMGX
46371 PARAOBJECT
46372 T_LINK
46373 STR_SPLIT
46374 SPLIT_TYPE
46375 SYS_PLSQL_95487_9_1
3> 執(zhí)行下面MERGE INTO 語(yǔ)句:
MERGE INTO YAG2 A
USING YAG1 B
ON (A.OBJECT_ID = B.OBJECT_ID)
WHEN MATCHED THEN
UPDATE SET A.OBJECT_NAME = B.OBJECT_NAME
WHEN NOT MATCHED THEN
INSERT VALUES (B.OBJECT_NAME, B.OBJECT_ID);
COMMIT;
4> MERGE INTO 更新后兩表的記錄對(duì)比:
SQL> SELECT A.OBJECT_ID,A.OBJECT_NAME,B.OBJECT_NAME FROM YAG1 A,YAG2 B WHERE A.OBJECT_ID=B.OBJECT_ID(+) ORDER BY 1;
A.OBJECT_ID A.OBJECT_NAME B.OBJECT_NAME
------------ ---------------- -----------------
46366 AAAAA AAAAA
46367 SUM_STRING SUM_STRING
46368 ARRAYLIST ARRAYLIST
46369 TYSKZ_SJDX TYSKZ_SJDX
46370 TYSKZ_SJXMGX TYSKZ_SJXMGX
46371 PARAOBJECT PARAOBJECT
46372 T_LINK T_LINK
46373 STR_SPLIT STR_SPLIT
46374 SPLIT_TYPE SPLIT_TYPE
46375 SYS_PLSQL_95487_9_1 SYS_PLSQL_95487_9_1
.
名稱(chēng)欄目:OracleMERGEINTO語(yǔ)法探究
分享路徑:http://jinyejixie.com/article8/gpesip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、搜索引擎優(yōu)化、服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)