Giter Club home page Giter Club logo

Comments (3)

dhensby avatar dhensby commented on June 9, 2024

Because you're using the tagged-template syntax, the library is trying to automatically parameterise your query, hence the error about param1_0.

As this fixture is trusted, you can skip parametirisation of the query by calling the function "traditionally":

import sql from 'mssql';

const fixtures = [
  { id: 1, name: 'Children Bicycles' },
  { id: 2, name: 'Comfort Bicycles' },
  { id: 3, name: 'Cruisers Bicycles' },
  { id: 4, name: 'Cyclocross Bicycles' },
  { id: 5, name: 'Electric Bikes' },
  { id: 6, name: 'Mountain Bikes' },
  { id: 7, name: 'Road Bikes' },
];

const pool = await sql.connect({ /* options */})
const transaction = pool.transaction();

try {
  await transaction.begin();

  const request = transaction.request();

  await request.batch`SET IDENTITY_INSERT production.categories ON`;

-  await request.batch`MERGE INTO production.categories AS target
+  await request.batch(`MERGE INTO production.categories AS target
  USING (
-    VALUES ${fixtures.map((category) => [category.id, category.name])}
+    VALUES ${fixtures.map((category) => `(${category.id}, '${category.name}')`).join(',\n')}
  ) AS source (id, name)
  ON target.id = source.id
  WHEN MATCHED THEN
    UPDATE SET name = source.name
  WHEN NOT MATCHED THEN
-    INSERT (id, name) VALUES (source.id, source.name);`;
+    INSERT (id, name) VALUES (source.id, source.name);`);

  await request.batch`SET IDENTITY_INSERT production.categories OFF`;
} catch (error) {
  console.error(error);
  await transaction.rollback();
} finally {
  await pool.close();
}

The library doesn't have any auto-parameterisation of arrays into values like that because the library is not a query builder and doesn't understand the context of the array being passed in (so it doesn't know to convert it to (@param0_1, @param0_2), (@param1_1, @param1_2), ... and instead just turns it into @param0_1, @param0_2, @param0_3, ... which isn't valid syntax for a MERGE statement.

tl;dr - your "built" query approach is correct.

from node-mssql.

leosuncin avatar leosuncin commented on June 9, 2024

@dhensby Is there a way to nest to use SQL fragments (like slonik does)? so you can tell the library not to escape an already escaped string

By example

await request.batch`MERGE INTO production.categories AS target
  USING (
    VALUES ${fixtures.map((category) => sql`(${category.id}, '${category.name}')`).join(',\n')}
  ) AS source (id, name)
  ON target.id = source.id
  WHEN MATCHED THEN
    UPDATE SET name = source.name
  WHEN NOT MATCHED THEN
    INSERT (id, name) VALUES (source.id, source.name);`;

from node-mssql.

dhensby avatar dhensby commented on June 9, 2024

No, there isn't.

from node-mssql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.