首页 > 代码库 > Oracle-Merge用法

Oracle-Merge用法

--创建测试表: 
CREATE TABLE MM (ID NUMBER, NAME VARCHAR2(20)); 
CREATE TABLE MN (ID NUMBER, NAME VARCHAR2(20)); 
--插入数据 
INSERT INTO MM VALUES (1, A); 
INSERT INTO MN VALUES (1, B);

--测试Merge
MERGE INTO MN A 
USING MM B 
ON(A.ID=B.ID) 
WHEN MATCHED THEN 
UPDATE SET A.NAME = B.NAME 
WHEN NOT MATCHED THEN 
INSERT(A.ID,A.NAME) VALUES(B.ID, B.NAME);

最终查询表A:MN,结果集为:

SQL> SELECT * FROM MN;
 
        ID NAME
---------- --------------------
         1 A

 

Oracle-Merge用法