简体   繁体   中英

How to bulk insert data into SQL Server using node.js

I want to insert 100k rows into a SQL Server database using node. I have tried it using bulk method of request object but it will not working for me.

My function to insert into SQL Server database:

 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); }); }); } 

This will return this error:

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

My recordset:

[ 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) } ]

Just in case anyone else stumbles across this question, I figured out my error, and what might be wrong for you.

Thanks to this other question that I found, I realized my error was that I was inserting the rows I wanted to add to an existing table, but you have to also insert the columns of the table you're trying to insert into. It looks like thats what your problem is here as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM