繁体   English   中英

如何使用nodejs在mssql表中插入批量数据

[英]How to Insert bulk Array of data in mssql table using nodejs

我正在使用节点 mssql,链接: 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 })}`)

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

使用批量

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

更好的答案:

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

更新威廉姆斯的回答:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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