Comments (3)
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.
@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.
No, there isn't.
from node-mssql.
Related Issues (20)
- The "config.server" property is required and must be of type string. HOT 1
- @azure/core-rest-pipeline breaking support for node.js 14 HOT 2
- Does node-mssql support multiSubnetFailover? HOT 9
- Suspected memory leak when streaming data HOT 4
- arrayRowMode returns a function for the column type HOT 6
- Problem with Persian/Arabic characters when using input() HOT 4
- Testing library-using code in Jest results in SyntaxError: Unexpected token 'export' HOT 1
- Request timeouts in mssql/msnodesqlv8 when using default Pool size HOT 4
- Bulk Insert row lock not supported HOT 1
- Committing a transaction early, within a transaction ? HOT 4
- [Question] Does node-mssql support Kerberos authentication HOT 1
- ConnectionError: Connection not yet open. HOT 1
- How to add order by from user input? HOT 1
- (node:1009) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [ConnectionPool]. Use emitter.setMaxListeners() to increase limit HOT 1
- Bulk Insert Time Out HOT 1
- Bulk upload with a DateTime2 column HOT 3
- error self signed certificate - Lambda function Nodejs connect to mssql on ec2 instance after migrating from 6.2.0 to 10.0.2 HOT 6
- node-mssql only works if TCP/IP for SQL Server is turned on HOT 2
- mssql suddenly stopped working with INSERTS to sql server db from AWS Lambda, with no changes anywhere. Had been working fine for years. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-mssql.