mybatis+sqlserver批量update操作數據庫報錯:The server supports a maximum of 2100 parameters


sqlserver中,update操作報錯,會判定長度是否超過2100,超過就會報錯,我這邊更新超過100就報錯了(我還需要研究一下為什么提交100條數據就報錯了,並沒有超過2100條啊)。

針對批量操作中出錯,解決辦法,如下:

 


 

1.xxxServiceImpl.java

 

 

 

 2.xxxMapper.java

 

 

3. xxxMapper.xml

    
    <update id="updatePoInfo"
        parameterType="java.util.List">
             <foreach collection="updatePoList" item="p" index="index" open="" close="" separator=";">
        update production_order
        <set>
        plant = #{p.plant},
        order_type = #{p.orderType},
        batch_no =#{p.batchNo},
        customer_code = #{p.customerCode},
        customer_name
        =#{p.customerName},
        material_no = #{p.materialNo},
        mode_no =#{p.moldNo},
        cust_material_no =
        #{p.custMaterialNo},
        order_qty =#{p.orderQty},
        uom =
        #{p.uom},
        packing_category =#{p.packingCategory},
        country = #{p.country},
        mpt
        =#{p.mpt},
        mpq = #{p.mpq},
        mpt_material_no =#{p.mptMaterialNo},
        length =
        #{p.length},
        width =#{p.width},
        high = #{p.high},
        apt =#{p.apt},
        apq = #{p.apq},
        assortment_id
        =#{p.assortmentID},
        assortment_description =
        #{p.assortmentDescription},
        total_carton_qty =#{p.totalCartonQty},
        rel_date
        =#{p.relDate},
        locco =
        #{p.locco},
        airplane_type =#{p.airplaneType},
        zscxs =
        #{p.zscxs}
</set>
        where
        production_order = #{p.productionOrderNo}
      </foreach>
    </update>

 

 

4.運行后報錯:

### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.
; uncategorized SQLException; SQL state [S0001]; error code [8003]; The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:89)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
    at com.sun.proxy.$Proxy89.update(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:294)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy3002.updatePoInfo(Unknown Source)
...................

 

 

 

 

修改后:

只用修改  xxxServiceImpl.java 就行:

 

 

代碼:

logger.info("生產訂單經過箱數字段為null檢驗過后,最終需要更新的po數據,共計:" + updatePoList.size() + "條");
                try {
                    if (!updatePoList.isEmpty() || updatePoList.size() > 0) {
                        //this.productionOrderMapper.updatePoInfo(updatePoList);// 更新箱數為空的po
                        
                        // 由於數據庫對於插入字段的限制為2100,在這里對批量插入的數據進行分批處理
                        int numms = updatePoList.size();
                        int sqlResult = 0;
                        List<PoTableVO> clientsnew = new ArrayList<>();
                        for (int j = 1; j <= updatePoList.size(); j++) {
                            clientsnew.add(updatePoList.get(j - 1));
                            if (j % 60 == 0) { // 每次插入數據庫的條數,建議值控制在60以內。
                                sqlResult = productionOrderMapper.updatePoInfo(clientsnew);
                                if (sqlResult > 0) {
                                    logger.info("箱數為null的po更新成功");
                                }
                                clientsnew.clear();// 清空clientsnew。
                            } else if (j == numms) {// 只插入最后一條。
                                sqlResult = productionOrderMapper.updatePoInfo(clientsnew);
                                if (sqlResult > 0) {
                                    logger.info("箱數為null的po更新成功");
                                }
                            }
                        }
                    }
                } catch (Exception e3) {
                    logger.error("箱數為null的po更新失敗" + e3.getMessage());
                    e3.printStackTrace();
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();// 回滾
                    return ResultRsp.ofFail(CodeEnum.FAIL_GETDATA);
                }

 

 

 

 

 

 

 

.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM