Ad
How To Insert Bulk Array Of Data In Mssql Table Using Nodejs
I am using node-mssql, LInk : https://www.npmjs.com/package/mssql
i want to insert bulk array of data in mssql database
i am getting array of array records eg: [[row1],[row2],[row3]]
i want to insert these records in mssql database
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 })}`)
Error : The label '@' has already been declared. Label names must be unique within a query batch or stored procedure.
Ad
Answer
Use 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
});
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad