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

3
  • If you're only looking for batch inserts, node-mssql can do bulk inserts. Commented Mar 17, 2020 at 22:19
  • npmjs.com/package/mssql#bulk-table-options-callback - It's there in the documentation. Commented Mar 18, 2020 at 3:22
  • I need to performa this using parameterized query, not table object. Commented Jun 1, 2020 at 22:22

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

mysql <> mssql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.