2015-12-16 93 views
0

我想使用節點將100k行插入到SQL Server數據庫中。我已經嘗試過使用請求對象的批量方法,但它不會爲我工作。如何使用node.js將數據批量插入到SQL Server中

我的功能,插入到SQL Server數據庫:

var startInsert = function(recordset) { 
 

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

 
\t \t recordset.forEach(function(row) { 
 
\t   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); 
 
\t   
 
\t \t }); 
 

 
\t \t var request = new sql.Request(); 
 

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

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

這將返回該錯誤:

{ [RequestError: Incorrect syntax near ')'.]
name: 'RequestError',
message: 'Incorrect syntax near \')\'.',
code: 'EREQUEST',
number: 102,
lineNumber: 1,
state: 1,
class: 15,
serverName: 'FCCHAKANDB',
procName: '',
precedingErrors: [] }

我的記錄:

[ 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: '[email protected]', 
    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) } ] 
+0

你的代碼附近有語法錯誤 –

+0

@Michelem: - 哪裏出錯? –

+0

Ohhh ... sry它是我在複製代碼時的錯誤... –

回答

0

爲了防止其他人絆倒這個問題,我想出了我的錯誤,以及可能是你的錯誤。

感謝這個我發現的其他question,我意識到我的錯誤是我插入了想要添加到現有表中的行,但是您還必須插入您嘗試插入的表的列成。它看起來像你的問題在這裏。