码迷,mamicode.com
首页 > 数据库 > 详细

Oracle之mergeInto

时间:2019-03-25 14:50:30      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:mybatis   date   oracl   ppi   The   ane   bat   oracle   update   

ORACLE  实现存在时 - 更新,不存在时,插入
1 MERGE INTO table B 
2 USING (SELECT COUNT(*) AS QUNATUM FROM table C WHERE C.CROSSID = 20000031100804AND 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……)

 

Oracle之mergeInto

标签:mybatis   date   oracl   ppi   The   ane   bat   oracle   update   

原文地址:https://www.cnblogs.com/assistants/p/10593504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!