在向数据库插入数据过程中,出现如下报错:
异常信息如下:
org.springframework.jdbc.UncategorizedSQLException:
Error updating database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 传入的请求具有过多的参数。该服务器支持最多 2100 个参数。请减少参数的数目,然后重新发送该请求。
The error occurred while setting parameters
SQL: INSERT INTO HIS_CHARGE_DETAIL_I_VIEW (HIS_PATIENT_ID, IN_HOS_ID, REC_NUMBER, NUMER, CHARGE_ORDER_DATE, ORDER_DEPT_CODE, ORDER_DEPT_NAME, ORDER_EMP_CODE, CHARGE_EXEC_DATE, EXEC_DEPT_CODE, EXEC_DEPT_NAME, EXEC_EMP_CODE, CHARGE_KIND_CODE, CHARGE_KIND_NAME, CHARGE_ITEM_CODE, CHARGE_ITEM_NAME, NUM, PRICE, AMOUNT, PATIENT_TYPE_CODE, PATIENT_TYPE_NAME ) values ( ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?,?, ?, ?,?, ?, ?, ?, ?, ?,? ) , ( ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?,?, ?, ?,?, ?, ?, ?, ?, ?,? )
解决方法:
通过网上查阅和测试,发现原因是一次向数据库传入参数超出限制
即 (list的size)乘(一行数据包含的参数)>2100
通过分批向数据库插入即可
int count = 90;
int batch = list.size() / count;
if (list.size() % count != 0) {
batch = batch + 1;
}
//循环批量保存每组数据
for (int i = 0; i < batch; i++) {
List<DemoEntity> subList = null;
if (i == batch - 1) {
subList = list.subList(count * i, list.size());
} else {
subList = list.subList(count * i, count * (i + 1));
}
JDBCMapper.addData(subList);
}
感谢阅读~