简体   繁体   English

将多条记录插入 node.js 中的 SQL 服务器(mssql)

[英]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.

相关问题 使用 Node.js 向 SQL 服务器插入多个条目 - Insert multiple entries to SQL Server with Node.js 在node.js中使用mssql进行多个查询 - multiple queries using mssql in node.js 在node.js中运行多个MSSQL查询 - Run multiple MSSQL queries in node.js 使用 node.js 在 sql 服务器数据库中一次插入多条记录时遇到麻烦 - Having troubles inserting multiple records at once without using for loop in sql server database using node.js 尝试使用 Node.js MSSQL 连接到 SQL 服务器数据库时出现错误 ELIFECYCLE 3228369023 - Error ELIFECYCLE 3228369023 on attempted connection to SQL Server database using Node.js MSSQL 如何在 node.js 中同步连接到 mssql 服务器 - How to connect to mssql server synchronously in node.js Node.js-从多个MSSQL查询返回数据 - Node.js - Returning data from multiple MSSQL queries 如何使用node.js在mySQL中从数组插入多个记录 - How do I insert multiple records from array in mySQL using node.js 如何使用Node.js中的Lua脚本将多个记录插入Redis哈希 - How to insert multiple records into Redis Hash using Lua Script in Node.js 将日期从javascript插入MSSQL Server时出现MSSQL Server Node.js错误 - MSSQL Server Node.js error when inserting date from javascript into MSSQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM