[DB] MyBatis(Oracle DB) 에서 다중 Insert 시 오류

seoyoon·2023년 3월 24일
0

0. 개요

Mybatis에서 foreach를 사용해서 다중 Insert를 하려고 할 때
'; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00933: SQL 명령어가 올바르게 종료되지 않았습니다.'
에러를 만났을 때 해결하는 방법을 알려드리려고 합니다.

1. 변경 전

<insert id="insertList" parameterType="java.util.Map">
	<foreach collection="list" item="item" index="index" separator=" " open="INSERT ALL" close="SELECT * FROM DUAL">
		INTO TEMP
			(
			NO,
          CONTENT,
			)
		VALUES
			(
			#{item.no},
          #{item.content}
			)
	</foreach>
</insert>

2. 변경 후

<update id="insertList" parameterType="java.util.Map">
	<foreach collection="list" item="item" index="index" separator=" " open="INSERT ALL" close="SELECT * FROM DUAL">
		INTO TEMP
			(
			NO,
         CONTENT,
			)
		VALUES
			(
			#{item.no},
         #{item.content}
			)
	</foreach>
</update>

위와 같이 MyBatis 에서 update 문으로 사용하지 않고 insert 문으로 다중 insert 를 시도할 시 아래와 같은 에러가 발생하므로
Oracle DB 이용시, update 문을 사용하면 됩니다.

profile
Backend Developer

0개의 댓글