Oracle B表快速更新A表
B表和A表是相同结构的表,A表由于有主外键的关系,没办法直接删除后,批量从B表插入,就需要通过inset 加上 update的方式来更新A表,传统的方式,是根据B表的ID和A表关联,然后更新值。
下面例子说明,传统的B表更新数据给A表
传统更新:update a set a.value = (select b.value from b where a.id = b.id)
传统新增:
下面介绍通过merge的方式快速更新和插入
merge into a using(select b.id,b.value from b) c on (a.id = c.id)
when matched then
update set a.value = c.value
when not matched then
insert(a.id,a.value)values(c.id,c.value)
以下是例子,先创建table1和table2,用table2覆盖table1
table1的数据

table2的数据

覆盖后的结果,应该是table1的数据和table2一样,执行合并

table1数据
ID | NAME |
---|---|
1 | C |
3 | E |
2 | D |
table2数据
ID | NAME |
---|---|
1 | C |
2 | D |
3 | E |
可以看到,id相同的情况下,table2的数据替换了table1,
table2没有找到table1的id情况下,table2新增了一条数据给table1
近期评论