Usage of merge into

1. Grammar

The syntax of merge into is as follows:

MERGE INTO [target-table] T USING [source-table sql] S ON([conditional expression] and [...]...)
WHEN MATCHED
THEN [UPDATE sql]
WHEN NOT MATCHED
THEN [INSERT sql]

Determine whether the source table S and the target table T meet the conditions in ON. If they meet, use the S table to update the T table. If not, insert the S table data into the T table. But there are many options, as follows:

  • Normal mode
  • Only update or only insert
  • Unconditional insert implementation
  • update with delete

2. Test table

-- 目标表
create table target
(
id NUMBER not null,
name VARCHAR2(12) not null,
year NUMBER
);

-- 源表
create table source
(
id NUMBER not null,
aid NUMBER not null,
name VARCHAR2(12) not null,
year NUMBER,
city VARCHAR2(12)
);
-- 插入测试数据
insert into target values(1,'liuwei',20);
insert into target values(2,'zhangbin',21);
insert into target values(3,'fuguo',20); 

insert into source values(1,2,'zhangbin',30,'吉林');
insert into source values(2,4,'yihe',33,'黑龙江');
insert into source values(3,3,'fuguo','','山东');

The query results of the two tables are as follows:

SQL> select * from target;

        ID NAME               YEAR
---------- ------------ ----------
         1 liuwei               20
         2 zhangbin             21
         3 fuguo                20

SQL> select * from source;

        ID        AID NAME               YEAR CITY
---------- ---------- ------------ ---------- ------------
         1          2 zhangbin             30 吉林
         2          4 yihe                 33 黑龙江
         3          3 fuguo                   山东

3. Normal mode

Now simply use merge into to implement the update operation when the conditions in on are met, otherwise the insert operation is performed. Check the target and source tables. The id in the target table is related to the source and aid. Now we need to realize that when the id of the target table matches the aid of the source, use the year of the source table to update the year of the target table. Otherwise, the year of the target table will be updated. The records in the source are inserted into the target table. The specific SQL implementation is as follows:

merge into target t using source s on (t.id = s.aid)
when matched then
update set t.year = s.year
when not matched then
insert values(s.aid, s.name, s.year);

The execution result is as follows:

SQL> select * from target;

        ID NAME               YEAR
---------- ------------ ----------
         1 liuwei               20 -- 原有记录
         2 zhangbin             30 -- 更新为 30
         3 fuguo                   -- 更新为 空
         4 yihe                 33 -- 新插入记录

4. Only update

Restore the data of the target table, and now implement the operation of using the year of the source table to update the year of the target table when the id of the target table matches the aid of the source. The specific implementation of SQL is as follows:

merge into target t using (select aid, name, year from source) s on (t.id = s.aid)
when matched then
update set t.year = s.year;

The execution result is as follows:

SQL> select * from target;

        ID NAME               YEAR
---------- ------------ ----------
         1 liuwei               20 -- 原有记录
         2 zhangbin             30 -- 更新为 30
         3 fuguo            	   -- 更新为 空

To restore the data in the target table, we can also add qualifying conditions in the update clause. For example, when using the year of the source table to update the year of the target table, in addition to the condition that the records in the source table match the target, the city must be It is from Jilin. The specific implementation of SQL is as follows:

merge into target t using source s on (t.id = s.aid)
when matched then
update set t.year = s.year where s.city = '吉林';

The execution result is as follows:

SQL> select * from target;

        ID NAME               YEAR
---------- ------------ ----------
         1 liuwei               20 -- 原有记录
         2 zhangbin             30 -- 更新为 30
         3 fuguo                20 -- 原有记录

5. Only  insert

Restore the data in the target table. Now, when the ID of the target table does not match the aid of the source, insert the records in the source into the target table. The specific SQL implementation is as follows:

merge into target t using source s on (t.id = s.aid)
when not matched then
insert(t.id, t.name, t.year) values(s.aid, s.name, s.year);

The execution result is as follows:

SQL> select * from target;

        ID NAME               YEAR
---------- ------------ ----------
         1 liuwei               20 -- 原有记录
         2 zhangbin             21 -- 原有记录
         3 fuguo                20 -- 原有记录
         4 yihe                 33 -- 新增记录

The insert clause can also add qualifications, similar to update, which will not be described here.

6. Unconditional insert

Restore the data of the target table. Sometimes we need to insert all the data in one table into another table. At this time, we can add a constant filter predicate to achieve it, so that it only meets matching and non-matching, so that there is only update or only insert. Here we want to insert all unconditionally, then we only need to set the condition in on to always false. The code to update the target using the source table is as follows:

merge into target t using source s on(1 = 0) -- 设置永假匹配条件
when not matched then
insert(t.id, t.name, t.year) values(s.aid, s.name, s.year);

The execution result is as follows:

SQL> select * from target;

        ID NAME               YEAR
---------- ------------ ----------
         1 liuwei               20
         2 zhangbin             21
         3 fuguo                20
         2 zhangbin             30 -- 新增记录
         4 yihe                 33 -- 新增记录
         3 fuguo        		   -- 新增记录

7. update with delete

Restore the data of the target table. Delete the records in the target table that match the specified conditions in the source table. It should be noted that the delete clause is attached to the update clause, that is, to have a delete clause, there must be an update clause. The following statement uses the records in the source table to match and update the records in the target table. At the same time, delete the record with id 2 in the target table among the matching records. The specific SQL implementation is as follows:

merge into target t using source s on(t.id = s.aid)
when matched then update set t.year = s.year
delete where(t.id = 2);

The execution result is as follows:

-- id 为 2 的记录被删除
SQL> select * from target;

        ID NAME               YEAR
---------- ------------ ----------
         1 liuwei               20 -- 原有记录
         3 fuguo                   -- year 被更新

Guess you like

Origin blog.csdn.net/weixin_44657888/article/details/124591434