Oracle数据库merge into的使用,存在则更新,不存在则插入

1、在实际应用场景中,我们会用到:如果这条数据在表中,就更新数据;如果不存在这条数据,就插入这条数据。
在oracle中,可以使用merge into实现,在mysql中可以使用ON DUPLICATE KEY UPDATE,这里只介绍oracle中的merge into实现方法,sql语法如下:

 
merge into testtable t using dual on (t.id = '1')  
         when not matched then insert (id,a,b) values (1,2,1)  
             when matched then update set b = b+1;  

select * from testtable;(如果testtable是空表,执行上述语句之后如下:) 
id    A    B
--------------- 
1     2    1

select * from testtable;(如果再执行一次merge into,testtable结果)
id    A    B
---------------
1     2    2

解释:

using dual on (…)是一种固定写法,也可以写成 using (select 1 from dual) on (…) , 表名dual也可以写成其他的名字。

on(…)里面是条件,可以写一个条件,也可以用and连多个条件。

2、顺便介绍一下mybatis中怎么使用merge into,直接上代码,代码使用的是动态sql。

<update id="updateStaffEvaluation" parameterType="java.util.HashMap">  
    merge into staff_evaluation t   
        using dual on (t.login_id = #{loginId,jdbcType=VARCHAR})  
        when not matched then insert  
    <trim prefix="(" suffix=")" suffixOverrides="," >  
        <if test="userId != null" >  
            USER_ID,  
        </if>  
        <if test="loginId != null" >  
            LOGIN_ID,  
        </if>  
        <if test="userName != null" >  
            USER_NAME,  
        </if>  
        <if test="complexScore != null" >  
            COMPLEX_SCORE,  
        </if>  
        <if test="mobileServiceScore != null" >  
            MOBILE_SERVICE_SCORE,  
        </if>  
        <if test="broadbandScore != null" >  
            BROADBAND_SCORE,  
        </if>  
        <if test="keepScore != null" >  
            KEEP_SCORE,  
        </if>  
        <if test="state != null" >  
            STATE,  
        </if>  
        <if test="evaluateText != null" >  
            EVALUATE_TEXT,  
        </if>  
        <if test="beginTime != null" >  
            BEGIN_TIME,  
        </if>  
        <if test="endTime != null" >  
            END_TIME,  
        </if>  
        <if test="evaluatePerson != null" >  
            EVALUATE_PERSON,  
        </if>  
    </trim>  
    <trim prefix="values (" suffix=")" suffixOverrides="," >  
        <if test="userId != null" >  
            #{userId,jdbcType=VARCHAR},  
        </if>  
        <if test="loginId != null" >  
            #{loginId,jdbcType=VARCHAR},  
        </if>  
        <if test="userName != null" >  
            #{userName,jdbcType=VARCHAR},  
        </if>  
        <if test="complexScore != null" >  
            #{complexScore,jdbcType=VARCHAR},  
        </if>  
        <if test="mobileServiceScore != null" >  
            #{mobileServiceScore,jdbcType=VARCHAR},  
        </if>  
        <if test="broadbandScore != null" >  
            #{broadbandScore,jdbcType=VARCHAR},  
        </if>  
        <if test="keepScore != null" >  
            #{keepScore,jdbcType=VARCHAR},  
        </if>  
        <if test="state != null" >  
            #{state,jdbcType=VARCHAR},  
        </if>  
        <if test="evaluateText != null" >  
            #{evaluateText,jdbcType=VARCHAR},  
        </if>  
        <if test="beginTime != null" >  
            #{beginTime,jdbcType=VARCHAR},  
        </if>  
        <if test="endTime != null" >  
            #{endTime,jdbcType=VARCHAR},  
        </if>  
        <if test="evaluatePerson != null" >  
            #{evaluatePerson,jdbcType=VARCHAR},  
        </if>  
    </trim>  
    when matched then update  
    <set>  
        <if test="complexScore != null" >  
            t.COMPLEX_SCORE = #{complexScore,jdbcType=VARCHAR},  
        </if>  
        <if test="mobileServiceScore != null" >  
            t.MOBILE_SERVICE_SCORE = #{mobileServiceScore,jdbcType=VARCHAR},  
        </if>  
        <if test="broadbandScore != null" >  
            t.BROADBAND_SCORE = #{broadbandScore,jdbcType=VARCHAR},  
        </if>  
        <if test="keepScore != null" >  
            t.KEEP_SCORE = #{keepScore,jdbcType=VARCHAR},  
        </if>  
        <if test="evaluateText != null" >  
            t.EVALUATE_TEXT = #{evaluateText,jdbcType=VARCHAR},  
        </if>  
        <if test="beginTime != null" >  
            t.BEGIN_TIME = #{beginTime,jdbcType=VARCHAR},  
        </if>  
        <if test="endTime != null" >  
            t.END_TIME = #{endTime,jdbcType=VARCHAR},  
        </if>  
        <if test="evaluatePerson != null" >  
            t.EVALUATE_PERSON = #{evaluatePerson,jdbcType=VARCHAR},  
        </if>  
    </set>  
    where t.LOGIN_ID = #{loginId,jdbcType=VARCHAR}  
</update>

注意:一定要用update标签。

    原文作者:SQL
    原文地址: https://segmentfault.com/a/1190000015281195
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞