转载请注明来源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/78421281,建议读者阅读原文,确保获得完整的信息
异常说明
由于上一个工程是Mysql+Mybatis,里面用到了批量插入SQL;而当前这个工程是Oracle+Mybatis,惯性思维就是把那段“foreach insert批量插入”SQL拷贝过来,结果运行就报错了。
执行的SQL语句如下:
<sql id="attachmentColumn">
id,
open_task_id,
category,
filename,
url
</sql>
<sql id="attachmentValue">
seq_aone_rus_hd_attachment.nextval,
#{item.open_task_id, jdbcType=INTEGER},
#{item.category, jdbcType=INTEGER},
#{item.filename, jdbcType=VARCHAR},
#{item.url, jdbcType=VARCHAR}
</sql>
<insert id="insert" useGeneratedKeys="true">
INSERT INTO AONE_RUS_HD_ISSUE_ATTACHMENT
(
<include refid="attachmentColumn" />
)
VALUES
<foreach collection="list" index="index" item="item" separator=",">
(
<include refid="attachmentValue" />
)
</foreach>
</insert>
运行报错如下:
### Error updating database. Cause: java.sql.SQLException: ORA-00933: SQL 命令未正确结束
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: INSERT INTO AONE_RUS_HD_ISSUE_ATTACHMENT ( id, open_task_id, category, filename, url ) VALUES ( seq_aone_rus_hd_attachment.nextval, ?, ?, ?, ? ) , ( seq_aone_rus_hd_attachment.nextval, ?, ?, ?, ? ) , ( seq_aone_rus_hd_attachment.nextval, ?, ?, ?, ? ) , ( seq_aone_rus_hd_attachment.nextval, ?, ?, ?, ? ) , ( seq_aone_rus_hd_attachment.nextval, ?, ?, ?, ? ) , ( seq_aone_rus_hd_attachment.nextval, ?, ?, ?, ? ) , ( seq_aone_rus_hd_attachment.nextval, ?, ?, ?, ? )
### Cause: java.sql.SQLException: ORA-00933: SQL 命令未正确结束
解决方法
报错后,单独把insert into table (keys) values (values),(values)…在客户端上执行也是不行的,发现oracle根本没有这样的语法,其正确的写法如下:
<insert id="insert" useGeneratedKeys="false">
INSERT INTO AONE_RUS_HD_ISSUE_ATTACHMENT
(
<include refid="attachmentColumn" />
)
SELECT seq_aone_rus_hd_attachment.nextval, A.*
FROM
(
<foreach collection="list" index="index" item="item" separator="UNION ALL">
SELECT
<include refid="attachmentValue" />
FROM dual
</foreach>
) A
</insert>
需要注意的几点如下:
- 1.数据表在没有设置主键时,useGeneratedKeys=”false”,不然会一直提示异常;
- 2.oracle中 separator=”UNION ALL”,而不像mysql中使用“,”即可;
- 3.需要把那些需要插入的值,构建成一张虚拟的表,如上述的表A。