Oracle之mergeInto

Posted 纸短情长,伏惟珍重.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle之mergeInto相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Oracle基础之Merge into

Oracle存储过程merge into

mybatis 使用oracle merge into 语句踩坑实录

oracle中merge into用法解析

ORACLE MERGE INTO

oracle中merge into用法解析