Oracle之mergeInto

ORACLE  实现存在时 - 更新,不存在时,插入
1 MERGE INTO table B 
2 USING (SELECT COUNT(*) AS QUNATUM FROM table C WHERE C.CROSSID = '20000031100804'AND C.LANEID = '' AND C.NEXTCROSSID = '') T 
3 ON (T.QUNATUM > 0) 
4 WHEN MATCHED THEN UPDATE SET B.SBBH = '',B.LANEID = '120',B.NEXTCROSSID = '20000031100805'
5 WHEN NOT MATCHED THEN INSERT (B.SBBH, B.LANEID, B.NEXTCROSSID,B.CROSSID) VALUES ('','5','20000031100806','20000031100810') ;

对应的Mybatis:

 1 <update id="mergeIntoLianeroadmapping" parameterType="java.lang.String">
 2         MERGE INTO table B
 3         USING (SELECT COUNT(C.CROSSID) AS QUNATUM FROM table C WHERE C.CROSSID = #{crossId}
 4         AND C.LANEID = #{laneId} AND C.NEXTCROSSID = #{nextCrossId}) T
 5         ON (T.QUNATUM > 0)
 6         WHEN MATCHED THEN
 7            UPDATE SET B.SBBH = '',B.LANEID = '6',B.NEXTCROSSID = #{nextCrossId}
 8         WHEN NOT MATCHED THEN
 9             INSERT (B.SBBH, B.LANEID, B.NEXTCROSSID,B.CROSSID) VALUES ('',#{laneId},#{nextCrossId},#{crossId})
10 </update>
 1 merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入。
 2 
 3 其基本语法规则是
 4 
 5 merge into 目标表 a
 6 
 7 using 源表 b
 8 
 9 on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)  
10 
11 when matched then update set a.更新字段=b.字段
12 
13 when  not macthed then insert into a(字段1,字段2……)values(值1,值2……)

 

posted @ 2019-03-25 14:22  闻长歌而知雅意  阅读(422)  评论(0编辑  收藏  举报