加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle 和 mysql 的批量操作Sql语句 的区别

发布时间:2020-12-12 13:17:13 所属栏目:百科 来源:网络整理
导读:正确的oracle批量新增的sql是: 方法 1: insert id="insertAttractionsBatch" parameterType="java.util.List" insert into ATTRACTIONS ( ID,NAME,?LONGITUDE,LATITUDE,??UPDATE_TIME ) ? foreach collection="list" item="item" index="index" separator=

正确的oracle批量新增的sql是:

方法 1:

<insert id="insertAttractionsBatch" parameterType="java.util.List">
insert into ATTRACTIONS (

ID,NAME,?LONGITUDE,LATITUDE,??UPDATE_TIME

)

? <foreach collection="list" item="item" index="index" separator="union all" >?
? ? ? (select ?
#{item.id,jdbcType=VARCHAR},#{item.name,#{item.longitude,jdbcType=DECIMAL},?#{item.updateTime,jdbcType=TIMESTAMP}
? ? ? ?from dual)
? ? </foreach>
</insert>

方法 2 :

<insert id="insertBatch" parameterType="java.util.List">
BEGIN
<foreach collection="list" item="item" index="index" separator=";" >
INSERT INTO TABLE.STUDENT (ID,AGE,STU_ID) VALUES
( DEMO.SEQ_EID.NEXTVAL,#{item.age},#{item.name},#{item.stuId} )
</foreach>
;END ;
</insert>

需要重点注意的是sql中没有values,和<foreach>标签中的(selece ..... from dual),MySql中的sql是这样的:

新增:

<insert id="insertAttractionsBatch" parameterType="java.util.List">
insert into ATTRACTIONS (

ID,??UPDATE_TIME

)??
? ? <foreach collection="list" item="item" index="index" separator="union all" >
#{item.id,jdbcType=TIMESTAMP}
? ? </foreach>
</insert>

oracle更新不能按普通的方式,需要这样:

<update id="updateAttractionsBatch" parameterType="java.util.List">
?? ?begin ?
?? ??? ?<foreach collection="list" item="item" index="index" separator=";" >
??????????? update ATTRACTIONS
?? ??? ??? ?<set>
?? ??? ??? ?<if test="item.id!=null and item.id!=‘‘">
?? ??? ??? ??? ?id = #{item.id},
?? ??? ??? ?</if>

??????????? <if test="item.head!=null and item.head!=‘‘">
?? ??? ??? ??? ?HEAD = #{item.head},
?? ??? ??? ?</if>

??????????? </set>
?? ??? ??? ?where id = #{item.id}
?? ??? ??? ?</foreach>
?? ??? ?;end;
?? ?</update>


删除就与MySql一样了如下:

<delete id="deleteAttractions" parameterType="java.util.List">? delete from ATTRACTIONS? <where>? <foreach collection="list" index="index" item="item" open="(" separator="or" close=")"> ? ??? id=#{item.id}? ? </foreach>? </where>? </delete>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读