SQL On duplicate key的使用

在SQL中实现接口的插入更新操作时,使用ONDUPLICATEKEY遇到问题。由于缺少IF判断,导致错误。修复后,通过添加IF条件检查字段是否存在,使用TRIM处理字段逗号,避免了语法错误。示例代码展示了如何在INSERTINTO和ONDUPLICATEKEYUPDATE语句中动态处理多个参数。
摘要由CSDN通过智能技术生成

场景:接口同时实现 插入更新操作,关于on duplicate key记录,使用时注意,存在多个更新参数时,又存在if 判断的前提下,需要使用对应的trim字段去替换信息。

踩到了坑:因为之前没有写if 判断,导致报错,检查后,添加if 又出现了,部分字段不存在,导致的结尾符号 逗号, 引发的报错,因此需要用trim去调整各个字段信息。 

        INSERT INTO msg_custom_message_sms_setting
        <trim prefix="(" suffix=")" suffixOverrides=",">
            `ID`,
            `TENANT_CODE`,
            `TEMPLATE_ID`,
            <if test="templateType!=null and templateType!=''">
                `TEMPLATE_TYPE`,
            </if>
            `LAST_UPDATE_DTM`,
            <if test="replaceParam!=null and replaceParam!=''">
                `REPLACE_PARAM`,
            </if>
            <if test="lastUpdateUser!=null and lastUpdateUser!=''">
                `LAST_UPDATE_USER`,
            </if>
            <if test="content!=null and content!=''">
                `CONTENT`,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <choose>
                <when test="id!=null and id!=''">
                    #{id},
                </when>
                <otherwise>
                    uuid(),
                </otherwise>
            </choose>
            #{tenantCode},
            #{templateId},
            <if test="templateType!=null and templateType!=''">
                #{templateType},
            </if>
            <choose>
                <when test="lastUpdateDtm!=null and lastUpdateDtm!=''">
                    #{lastUpdateDtm},
                </when>
                <otherwise>
                    now(),
                </otherwise>
            </choose>
            <if test="replaceParam!=null and replaceParam!=''">
                #{replaceParam},
            </if>
            <if test="lastUpdateUser!=null and lastUpdateUser!=''">
                #{lastUpdateUser},
            </if>
            <if test="content!=null and content!=''">
                #{content},
            </if>
        </trim>
        on duplicate key update
        <trim suffixOverrides=",">
            <if test="templateId!=null and templateId!=''">
                TEMPLATE_ID = VALUES(TEMPLATE_ID),
            </if>
            <if test="lastUpdateDtm!=null and lastUpdateDtm!=''">
                LAST_UPDATE_DTM = VALUES(LAST_UPDATE_DTM),
            </if>
            <if test="replaceParam!=null and replaceParam!=''">
                REPLACE_PARAM = VALUES(REPLACE_PARAM),
            </if>
            <if test="lastUpdateUser!=null and lastUpdateUser!=''">
                LAST_UPDATE_USER = VALUES(LAST_UPDATE_USER),
            </if>
            <if test="content!=null and content!=''">
                CONTENT = VALUES(CONTENT),
            </if>
        </trim>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值