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;优点:
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;
转至:http://blog.csdn.net/nsj820/article/details/5755685