如何使用node.js将数据批量插入SQL Server

时间:2015-12-16 07:26:52

标签: node.js sql-server-2008

我想使用node将100k行插入SQL Server数据库。我已经尝试使用请求对象的批量方法,但它对我不起作用。

我的插入SQL Server数据库的功能:



var startInsert = function(recordset) {

    sql.connect(config,function(err) {
	 	  // console.log(recordset);
	 	  if(err){
	       	console.log(err);
	      }
	        
 	      var table = new sql.Table('ShipmentAuditLogTest');

	  	  recordset.forEach(function(row) {
	           table.rows.add(row.ShippingID,row.BagNo,row.ProcessLocation,row.Process,row.Comment,row.CreatedDate,row.CreatedBy,row.LastModifiedDate,row.LastModifiedBy,row.DestinationLocation,row.VenderLostShipmentsDebitId);
	        
	  	  });

	  	  var request = new sql.Request();

		 request.bulk(table, function(err, rowCount) {
			console.log(table);

	        if(err)
	        	console.log(err);
	        console.log(rowCount);
	     });
    });
}




这将返回此错误:

  

{[RequestError:'附近的语法错误)'。]   名称:' RequestError',
  消息:')'附近的语法不正确。'。',
  代码:' EREQUEST',
  数量:102,
  lineNumber:1,
  州:1,
  课程:15,
  serverName:' FCCHAKANDB',
  procName:'',
  前述错误:[]}

我的记录集:

[ RowDataPacket {
    ShippingID: '2FX880141',
    BagNo: 'CHKXFL1808362',
    ProcessLocation: '2',
    Process: 'BagIn',
    Comment: 'Shipment added in ParentBagNo: CHKXFL1808362',
    CreatedDate: Tue Apr 07 2015 16:36:18 GMT+0530 (India Standard Time),
    CreatedBy: 'support@firstcry.com',
    LastModifiedDate: '0000-00-00 00:00:00',
    LastModifiedBy: 'null',
    DestinationLocation: 'null',
    VenderLostShipmentsDebitId: 0,
    InsertedTime: Mon Dec 14 2015 14:26:16 GMT+0530 (India Standard Time) } ]

1 个答案:

答案 0 :(得分:0)

以防万一其他人偶然发现这个问题,我弄清楚了我的错误,以及可能对你有什么不妥。

感谢我找到的其他question,我意识到我的错误是我插入了我想要添加到现有表中的行,但您还需要插入表格的列#39 ;试图插入。看起来这就是你的问题所在。