MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。
通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,
连接条件匹配上的进行UPDATE,无法匹配的执行INSERT。
这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE。
*/
/*語法:
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;
*/
语法:
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 ! ]
/*
我们还是以《sql中的case应用》中的表为例。在创建另两个表fzq1和fzq2
*/
--全部男生记录
create table fzq1 as select * from fzq where sex=1;
--全部女生记录
create table fzq2 as select * from fzq where sex=0;
1、/*涉及到两个表关联的例子*/
--更新表fzq1使得id相同的记录中chengji字段+1,并且更新name字段。
--如果id不相同,则插入到表fzq1中.
--将fzq1表中男生记录的成绩+1,女生插入到表fzq1中
merge into fzq1 aa --fzq1表是需要更新的表
using fzq bb -- 关联表
on (aa.id=bb.id) --关联条件
when matched then --匹配关联条件,作更新处理
update set
aa.chengji=bb.chengji+1,
aa.name=bb.name --此处只是说明可以同时更新多个字段。
when not matched then --不匹配关联条件,作插入处理。如果只是作更新,下面的语句可以省略。
insert values( bb.id, bb.name, bb.sex,bb.kecheng,bb.chengji);
select * from fzq1;
merge into fzq1 aa --fzq1表是需要更新的表
using (select fzq.id,fzq.chengji
from fzq join fzq2
on fzq.id=fzq2.id) bb -- 数据集
on (aa.id=bb.id) --关联条件
when matched then --匹配关联条件,作更新处理
update set
aa.chengji=bb.chengji+1
select * from fzq1;
3、不能做的事情
merge into fzq1 aa
using fzq bb
on (aa.id=bb.id)
when matched then
update set
aa.id=bb.id+1
/*系统提示:
更新方法使用update
update fzq1
set id=(select id+1 from fzq where fzq.id=fzq1.id)
where id in
(select id from fzq)
转自:http://blog.csdn.net/nsj820/article/details/5755685
Oracle9i引入了MERGE命令,你能够在一个SQL语句中对一个表同时执行inserts和updates操作. MERGE命令从一个或多个数据源中选择行来updating或inserting到一个或多个表.在Oracle10g中MERGE有如下一些改进:
1、UPDATE或INSERT子句是可选的
2、UPDATE和INSERT子句可以加WHERE子句
3、在ON条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表
4、UPDATE子句后面可以跟DELETE子句来去除一些不需要的行
语法:
MERGEHINTINTO SCHEMA .TABLE T_ALIAS
USINGSCHEMA . {TABLE | VIEW |SUBQUERY } T_ALIAS
ON (CONDITION)
WHENMATCHEDTHEN MERGE_UPDATE_CLAUSE
WHENNOTMATCHED THEN MERGE_INSERT_CLAUSE / MERGE_DELETE_CLAUSE;
联想:
merge into是特有的功能,相当于在 MSSQL中的
ifexists(...)
updatetable
else
Insertinto table.
mergeinto语法不仅没有if exists语法啰嗦,而且比if exists还要高效很多,常用来在oracle之间同步数据库表。
例子:
1、创建测试表及数据
DROP TABLE PRODUCTS;
DROP TABLE NEWPRODUCTS;
create table PRODUCTS
(
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2(60),
CATEGORY VARCHAR2(60)
);
insert into PRODUCTS values (1501, 'VIVITAR 35MM', 'ELECTRNCS');
insert into PRODUCTS values (1502, 'OLYMPUS IS50', 'ELECTRNCS');
insert into PRODUCTS values (1600, 'PLAY GYM', 'TOYS');
insert into PRODUCTS values (1601, 'LAMAZE', 'TOYS');
insert into PRODUCTS values (1666, 'HARRY POTTER', 'DVD');
commit;
create table NEWPRODUCTS
(
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2(60),
CATEGORY VARCHAR2(60)
);
insert into NEWPRODUCTS values (1502, 'OLYMPUS CAMERA', 'ELECTRNCS');
insert into NEWPRODUCTS values (1601, 'LAMAZE', 'TOYS');
insert into NEWPRODUCTS values (1666, 'HARRY POTTER', 'TOYS');
insert into NEWPRODUCTS values (1700, 'WAIT INTERFACE', 'BOOKS');
commit;
2、匹配更新
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,
P.CATEGORY = NP.CATEGORY;
SELECT * FROM PRODUCTS;
SELECT * FROM NEWPRODUCTS;
3、不匹配插入
MERGE INTO PRODUCTS P
USING NEWPRODUCTS NP
ON (P.PRODUCT_ID = NP.PRODUCT_ID)
WHEN NOT MATCHED THEN
INSERT
(PRODUCT_ID
,PRODUCT_NAME
,CATEGORY)
VALUES
(NP.PRODUCT_ID
,NP.PRODUCT_NAME
,NP.CATEGORY);
SELECT * FROM PRODUCTS;
SELECT * FROM NEWPRODUCTS;
4、匹配带where/on更新
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 WHERE P.CATEGORY = NP.CATEGORY;
MERGE INTO PRODUCTS P
USING NEWPRODUCTS NP
ON (P.PRODUCT_ID = NP.PRODUCT_ID AND P.CATEGORY = NP.CATEGORY)
WHEN MATCHED THEN
UPDATE SET P.PRODUCT_NAME = NP.PRODUCT_NAME;
SELECT * FROM PRODUCTS;
SELECT * FROM NEWPRODUCTS;
SELECT *
FROM PRODUCTS A
INNER JOIN NEWPRODUCTS B
ON A.PRODUCT_ID = B.PRODUCT_ID
AND A.CATEGORY = B.CATEGORY;
5、匹配带where更新、插入
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,
P.CATEGORY = NP.CATEGORY
WHERE P.CATEGORY = 'DVD'
WHEN NOT MATCHED THEN
INSERT
(PRODUCT_ID
,PRODUCT_NAME
,CATEGORY)
VALUES
(NP.PRODUCT_ID
,NP.PRODUCT_NAME
,NP.CATEGORY) WHERE NP.CATEGORY != 'BOOKS';
SELECT * FROM PRODUCTS;
SELECT * FROM NEWPRODUCTS;
6、ON常量表达式
MERGE INTO PRODUCTS P
USING NEWPRODUCTS NP
ON (1 = 0)
WHEN NOT MATCHED THEN
INSERT
(PRODUCT_ID
,PRODUCT_NAME
,CATEGORY)
VALUES
(NP.PRODUCT_ID
,NP.PRODUCT_NAME
,NP.CATEGORY) WHERE NP.CATEGORY = 'BOOKS';
SELECT * FROM PRODUCTS;
SELECT * FROM NEWPRODUCTS;
7、匹配删除、不匹配插入
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,
P.CATEGORY = NP.CATEGORY DELETE
WHERE (P.CATEGORY = 'ELECTRNCS')
WHEN NOT MATCHED THEN
INSERT
(PRODUCT_ID
,PRODUCT_NAME
,CATEGORY)
VALUES
(NP.PRODUCT_ID
,NP.PRODUCT_NAME
,NP.CATEGORY);
SELECT * FROM PRODUCTS;
SELECT * FROM NEWPRODUCTS;
8、源表为子查询(自联接)
MERGE INTO PRODUCTS P
USING (SELECT COUNT(*) CO FROM PRODUCTS WHERE PRODUCTS.PRODUCT_ID = 1501) B
ON (B.CO <> 0)
WHEN MATCHED THEN
UPDATE SET P.PRODUCT_NAME = 'KEBO' WHERE P.PRODUCT_ID = 1501
WHEN NOT MATCHED THEN
INSERT
(PRODUCT_ID
,PRODUCT_NAME
,CATEGORY)
VALUES
(1501
,'KEBO'
,'NBA');
MERGE INTO PRODUCTS P
USING (SELECT COUNT(*) CO FROM PRODUCTS WHERE PRODUCTS.PRODUCT_ID = 1508) B
ON (B.CO <> 0)
WHEN MATCHED THEN
UPDATE SET P.PRODUCT_NAME = 'KEBO' WHERE P.PRODUCT_ID = 1508
WHEN NOT MATCHED THEN
INSERT
(PRODUCT_ID
,PRODUCT_NAME
,CATEGORY)
VALUES
(1508
,'KEBO'
,'NBA');
SELECT * FROM PRODUCTS;
9、带delete语句选项的示例
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, P.CATEGORY = NP.CATEGORY
--WHERE (P.CATEGORY = 'ELECTRNCS')
DELETE
WHERE (P.CATEGORY = 'ELECTRNCS')
WHEN NOT MATCHED THEN
INSERT
(PRODUCT_ID, PRODUCT_NAME, CATEGORY)
VALUES
(NP.PRODUCT_ID, NP.PRODUCT_NAME, NP.CATEGORY);
SELECT * FROM PRODUCTS;
SELECT * FROM NEWPRODUCTS;
说明:
DELETE语句删除的是满足matched关联on条件,同时也要是update更新内容的子集,否则不会删除任何内容。
优点:
—避免了分开更新
—提高性能并易于使用
—在数据仓库应用中十分有用
—使用merge比传统的先判断再选择插入或更新快很多
需要注意的地方:
1、从语法条件上看(ON (join condition)),merge into也是一个危险的语法。如果不慎重使用,会将源表全部覆盖到目标表,既危险又浪费效率,违背了增量同步的原则。所以在设计表结构时,一般每条记录都有“更新时间”的字段,用目标表“最大更新时间”判断源表数据是否有更新和新增的信息。
2、更新的字段,不允许有关联条件的字段(join condition)。比如条件是 A.ID=B.ID,那么使用“SET A.ID=B.ID”将报出一个莫名其妙的提示错误。