使用TypeORM批量保存数据时报错:请求参数过多
问题:TypeORM批量保存SQL Server触发参数数量超限错误
在NodeJS应用中用TypeORM连接SQL Server,批量存储200多条数据时触发报错,已尝试配置chunk参数但未解决问题。
报错提示:
The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.
相关代码:
let csvDataArr = data.map(record => { return { itemType: parseInt(record['itemType']), dateTime: !isNaN(Date.parse(record['dateTime'])) ? new Date(record['dateTime']) : null, barcode: record['barcode'], description: record['description'], specialInformation: record['specialInformation'], signedBy: record['signedBy'], location: record['location'], locationExternalReference: record['locationExternalReference'], scanLatitude: record['scanLatitude'], scanLongitude: record['scanLongitude'], clientSpecificOrderRef: record['clientSpecificOrderRef'], deliveryID: record['deliveryID'] } }) return await this.repo.save(csvDataArr, { chunk: 500 });
解答
问题根源
你设置的chunk:500太大了。每条数据包含12个字段,500条数据对应的参数总数是500*12=6000,远远超过SQL Server限制的2100个参数上限,因此触发报错。
解决步骤
- 计算合适的chunk大小:用SQL Server的参数上限2100除以单条数据的字段数(这里是12),得到
2100/12=175,所以chunk值应设为175或更小(比如150),留有余量避免意外。 - 修改代码中的chunk参数:
return await this.repo.save(csvDataArr, { chunk: 150 });
- 验证效果:调整后每次批量提交的参数数量为
150*12=1800,低于2100的限制,即可正常执行批量保存。
额外说明
TypeORM的chunk参数控制的是每次批量插入的记录数,而非参数总数。SQL Server对单条SQL语句的参数数量有严格限制,因此必须根据单条记录的字段数反推合适的chunk值,不能随意设置过大的数值。
内容的提问来源于stack exchange,提问作者shAkur
相关产品推荐
相关产品推荐

