merge into的形式:
MERGE INTO [target-table] A USING [source-table sql] B ON([conditional expression] and […]…)
WHEN MATCHED THEN
[UPDATE sql]
WHEN NOT MATCHED THEN
[INSERT sql]
作用:判断B表和A表是否满足ON中条件,如果满足则用B表去更新A表,如果不满足,则将B表数据插入A表但是有很多可选项,如下:
1.正常模式
2.只update或者只insert
3.带条件的update或带条件的insert
4.全插入insert实现
5.带delete的update(觉得可以用3来实现)
eg:
merge into 表1 a using (select hh,xse,mle from zzjtwo.ml) b on (a.subbh=b.hh and a.fyear=‘2020’ and a.fmonth=‘6’)
when matched
then update set a.mle=nvl(a.mle,0)+nvl(b.mle,0),a.xse=nvl(a.xse,0)+nvl(b.xse,0)
when not matched
then insert (subbh,xse,mle,fyear,fmonth) values (b.hh,b.xse,b.mle,‘2020’,‘6’)