4

In java I can send multiple set of parameters of a parameterized query in one go (see code example below).

How can I accomplish the same result in NodeJs, using node-mssql package?

  PreparedStatement ps = c.prepareStatement("INSERT INTO employees VALUES (?, ?)");
  ps.setString(1, "John");
  ps.setString(2,"Doe");
  ps.addBatch();
  ps.clearParameters();
  ps.setString(1, "Dave");
  ps.setString(2,"Smith");
  ps.addBatch();
  ps.clearParameters();
  int[] results = ps.executeBatch();

Dale K
  • 25,246
  • 15
  • 42
  • 71

2 Answers2

1

You could bulk insert using request.bulk method.

This example is node-mssql driver package.

bulk (table, [options,] [callback])
Perform a bulk insert.

Arguments

table - sql.Table instance.

options - Options object to be passed through to driver (currently tedious only). Optional. If argument is a function it will be treated as the callback.

callback(err, rowCount) - A callback which is called after bulk insert has completed, or an error has occurred. Optional. If omitted, returns Promise.

Example:

const request = new sql.Request(/* [pool or transaction] */)

try {
    const table = new sql.Table('employees ') // or temporary table, e.g. #temptable
    table.rows.add(1, "John");
    table.rows.add(2,"Doe");
    table.rows.add(1, "Dave");
    table.rows.add(2,"Smith");

    const request = new sql.Request()
    await request.bulk(table);
} catch(error) {
    console.error(error.message);
}

You could read more about bulk insert here

Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42
0

Not sure if this helps.

See if it is useful:

https://blog.stvmlbrn.com/2018/07/27/dynamic-where-clauses-with-parameterized-queries-in-node-mysql.html