如何使用Node.js在MSSQL表中插入大数据数组

时间:2019-06-21 08:07:58

标签: javascript sql node.js sql-server express

我正在使用node-mssql,LInk:https://www.npmjs.com/package/mssql

我想在mssql数据库中插入大量数据

我正在获取数组记录的数组,例如:[[row1],[row2],[row3]]

我想在mssql数据库中插入这些记录

import * as sql from "mssql";

const conn = new sql.ConnectionPool({
   user: "XXXXXXXXX",
   password: "XXXXXXXXXXX",
   server: "XXXXXXXXXXX",
   database: "TESTDATA",
   options: {
    instanceName: "XXX"
   },
   pool: {
     max: 10,
     min: 0,
     idleTimeoutMillis: 30000
   }
 });

conn.connect()

var values = [[john,1,4,80],[jenny,2,4,78],[abhi,3,4.60],[ram,4,4,90]]

new sql.Request(conn)
    .query(`INSERT INTO CLASS_TABLE (NAME, ROLL, CLASS, MARKS) VALUES
 ${values.map(row=>{ var num = Array('(' + row.join() + ')' ).join(); return num })}`)

错误:标签'@'已经被声明。标签名称在查询批处理或存储过程中必须唯一。

3 个答案:

答案 0 :(得分:0)

使用bulk

const table = new sql.Table('CLASS_TABLE');
table.columns.add('NAME', sql.NVarChar(15), {nullable: false});
table.columns.add('ROLL', sql.Int, {nullable: false});
table.columns.add('CLASS', sql.Int, {nullable: false});
table.columns.add('MARKS', sql.Int, {nullable: false});

values.forEach(arr => table.rows.add.apply(null, arr));

const request = new sql.Request();
request.bulk(table, (err, result) => {
  // ... error checks
});

答案 1 :(得分:0)

更好的答案:

import {
  ConnectionPool,
  Table,
  VarChar,
  Int,
} from 'mssql';

var values = [[john,1,4,80],[jenny,2,4,78],[abhi,3,4,60],[ram,4,4,90]];

const table = new Table('CLASS_TABLE');
table.create = false;
// Ignore the identity field
table.columns.add('NAME', VarChar, { nullable: true });
table.columns.add('ROLL', Int, { nullable: true });
table.columns.add('CLASS', Int, { nullable: true });
table.columns.add('MARKS', Int, { nullable: true });

for (let j = 0; j < values.length; j += 1) {
  table.rows.add(
    values[j][0],
    values[j][1],
    values[j][2],
    values[j][3]
  );
}
const request = pool.request();

const results = await request.bulk(table);
console.log(`rows affected ${results.rowsAffected}`);

答案 2 :(得分:0)

更新为威廉姆斯答案:

import {
ConnectionPool,
Table,
VarChar,
Int,
} from 'mssql';
let values = [[john,1,4,80],[jenny,2,4,78],[abhi,3,4,60],[ram,4,4,90]];
const table = new Table('CLASS_TABLE');
table.create = false;
// Ignore the identity field
table.columns.add('NAME', VarChar, { nullable: true });
table.columns.add('ROLL', Int, { nullable: true });
table.columns.add('CLASS', Int, { nullable: true });
table.columns.add('MARKS', Int, { nullable: true });
//If you have an array of arrays you can map it without hard coding the amount of values needed
//for (let j = 0; j < values.length; j += 1) {
// table.rows.add(
//    values[j][0],
//    values[j][1],
//    values[j][2],
//    values[j][3]
//  );
//}

//Updated way allows for dynamically added columns coming from something like a config file
values.forEach(row => table.rows.add.apply(table.rows,row));
const request = pool.request();
const results = await request.bulk(table);
console.log(`rows affected ${results.rowsAffected}`);