oracle merge into on using 用法
MERGE INTO table1 a USING ( select id,name from table2) b ON (a.id=b.id) WHEN MATHED THEN update set a.name=b.name WHEN NOT MATHED THEN insert (id,name) values('id','name');
merge into 一般用于主键存在就更新其他字段,主键不存在就新增一条数据。
MATHED then后面的 update 不需要写表名,并且可以添加where条件,是对匹配到的结果集进行筛选。update 后面一般不用条件,其更新操作只对匹配到的结果集更新。
DECLARE V_LANG CLOB := '超长字符串'; BEGIN merge into LM_BIDDING a using ( select NVL('甘肃省XXXXXXXXXX', '0' ) as title ,'TS00000063' as custno ,NVL('2018-11-12', '0' ) as releasedate ,NVL('甘肃省zz', '0' ) as purchaser from dual) b on (NVL( a.TITLE, '0' )=b.title and a.CUSTNO=b.custno and NVL( a.RELEASEDATE, '0' )=b.releasedate and NVL( a.PURCHASER, '0' )=b.purchaser ) WHEN MATCHED THEN UPDATE SET AMDCNL=AMDCNL||'+1' --WHERE CUSTNO='TS00000063' AND RELEASEDATE='2018-11-12' AND TITLE='甘肃省XXXXXXXXXX' AND PURCHASER='甘肃省zz'
when not matched then insert ( BIDDINGID, CUSTNO, CREDITCODE, AGENCY, ANNODATE, ANNOITEM ) values ( 'Bxxxxx440', 'TS0xxxx063', '91620xxxx74835J', '甘肃xxxx有限公司', '2018-11-12', ' 服务/环境服务/空气污染治理服务') ;
update LM_BIDDING set TEXT=TEXT||V_LANG where BIDDINGID = #{biddingid,jdbcType=VARCHAR} ;
END;