I want to insert many rows at once with an array data.
My array looks like this
[ [ '1234' ],
[ '5678' ],
[ '9123' ]... ]
and my query code
const sql = require('mssql');
const config = {
user: 'sa',
password: 'pass',
server: 'ip',
database: 'db'
};
async function get_numbers() {
try {
let pool = await new sql.ConnectionPool(config).connect();
var qstring = `INSERT INTO numbers (gen_number) VALUES ?`;
pool.request().query(qstring, mins, function (err, result) {
if (err) throw err;
});
//pool.close();
} catch (err) {
console.log(err);
}
};
But this gives incorrect syntax near ? error.
I assume that you want to insert the array contents 1234,5678 ..
into the db. You can use
the following query
var array = [ [ '1234' ], [ '5678' ], [ '9123' ]... ];
var query = `var query = `INSERT INTO numbers (gen_number) VALUES ${array.join().split(",").map(i => '(' + i + ')').join()}``
This is simply joining the array contents and giving you a string that matches the SQL
syntax to insert multiple values in one statement.
The above query will result into something like
"INSERT INTO numbers (gen_number) VALUES 1234,5678,9123"