您的位置:首页 > 博客中心 > 数据库 >

merge into Oracle里的 saveOrUapdate

时间:2022-03-14 01:42

1、初始数据:

SQL> select * from a;
 
ID                     NAME
---------------------- ----------------------
1                      1
2                      1
3                      1
4                      1
5                      1
SQL> select * from b;
 
ID                     NAME
---------------------- ----------------------
1                      2
2                      2
11                     11

2、目标:1)a表中id与b表中id一样的话,更新a.name以b.name为准;2)b表中id不在a表中的话,复制b表的记录,插入到a表中

3、功能实现:

merge into A a using B b on (a.id = b.id) --on为匹配条件
when matched then --匹配时 更新a.name
update set a.name = b.name where 1=1 --这里可以添加where条件
when not matched then --不匹配时 复制并插入b
insert values(b.id,b.name) where 1=1

  

 

热门排行

今日推荐

热门手游