[英]Insert multiple records into SQL Server (mssql) in node.js
I am migrating a piece of code from mysql to mssql package of nodejs, in which its required to insert multiple rows.我正在将一段代码从 mysql 迁移到 nodejs 的 mssql package,其中需要插入多行。
Here's the sample code I am using for testing:这是我用于测试的示例代码:
const db = require('mssql');
let config = {
user: 'salim',
password: 'admin',
server: 'LAPTOP-JK45R', // You can use 'localhost\\instance' to connect to named instance
database: 'master',
}
var pool;
//initialize connection pool
var connectPool = initializeConnectionPool();
function initializeConnectionPool() {
pool = new db.ConnectionPool(config);
pool.on('error', (err) => {
logger.error(err);
});
return pool.connect();;
}
connectPool.then(async () => {
let connection = await pool.request();
console.log('Got pool connection...');
var q = "INSERT INTO Person (name, address) VALUES ?";
var values = [
['John', 'Highway 71'],
['Peter', 'Lowstreet 4'],
['Amy', 'Apple st 652'],
['Hannah', 'Mountain 21']
];
let result = await connection.query(q,[values]);
console.log(`Result: ${JSON.stringify(result)}`);
});
Its giving me error:它给了我错误:
RequestError: Incorrect syntax near '?'. RequestError:“?”附近的语法不正确。
I couldn't find any thing on official npm page of mssql
, so I have been trying this: Insert multiple columns and rows into SQL Server with node js我在mssql
的官方 npm 页面上找不到任何东西,所以我一直在尝试这个: Insert multiple columns and rows into SQL Server with node js
In my code I am just using pool.在我的代码中,我只是使用池。
I also couldn't find how to log queries using this package, so couldn't figure out what the query is being formed.我也找不到如何使用此 package 记录查询,因此无法弄清楚查询正在形成什么。
It would be great to know any of the solution.很高兴知道任何解决方案。
The ?
?
is a way to pass parameter in mysql是一种在 mysql 中传递参数的方法
In mssql it seem to be like ${entries}
在 mssql 中,它似乎像${entries}
refer to How to pass parameter to mssql query in node js请参阅如何在节点 js 中将参数传递给 mssql 查询
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.