0

Following is a bulk update query i have written using foreach and update statement. In this apart from update_time any other parameter can be null. This query is supposed to take a list of objects as parameter and return void.

<update id="bulkUpdate" parameterType="java.util.List">
<foreach collection="list" item="item"  index="index"  separator=";" >
    UPDATE 
     <include refid="tableName" /> 
    <set>
             update_time=#{item.updateTime}
            <if test="item.testFlg != null">, test_flg=#{item.testFlg}</if>
            <if test="item.DueDate != null">, due_date=#{item.DueDate}</if>
            <if test="item.versionId != null">, version_id=#{item.versionId}</if>
     </set>
     WHERE
    <include refid="tableName" />.order_id=#{item.orderId} 
</foreach>
</update>

After Debugging i found that the query is getting all the required not null values correctly. However I am getting this error that is driving me crazy.

The error occurred while setting parameters\r\n### SQL: UPDATE        glb_order_tbl        SET update_time=?                                                                                                                                                                                                   , complete_due_date=?                          , version_id=?       WHERE      glb_order_tbl .order_id=? ;             UPDATE        glb_order_tbl        SET update_time=?                                                                                                                                                                                                   , complete_due_date=?                          , version_id=?       WHERE      glb_order_tbl .order_id=? \r\n### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE \n      glb_order_tbl  \n     SET update_time='2015-02-24 13:01:48.608'\n   ' at line 24\n; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE \n      glb_order_tbl  \n     SET update_time='2015-02-24 13:01:48.608'\n   ' at line 24"

It seems some sort of syntactical error that i can't find out.
I am using Java+spring+MyBatis+MySql

Updated Query and error.Please note that parameters being set(inside set block) might have changed

Thanks in advance.

1 Answer 1

1

The problem here is that you set the open, close and separator incorrectly in your foreach closure.

In your sql, it appends ( at the beginning of the whole sql, and ) at the end, and separate each update sql with ,. The generated sql surely has the syntactical error.

Change it as follows, and it should work.

<foreach collection="list" item="item" index="index" separator=";">
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Landys. I was able to figure that out after properly understanding foreach statement and modified my statement as per your answer .However its still giving same error although on different line . And i was able to do multiple updates using the "wrong" query via workbench. I would like to point out that i am using simple mybatis executor .Do i need to use batch executor or something ..??
do you enable the multiple update in the jdbc url? If not ,you should append the parameter allowMultiQueries=true into the url of jdbc .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.