如何将 sql 文件传递到 node-mssql
How to pass sql file into node-mssql
我们如何将外部 sql 文件传递到 node-mssql 查询
我尝试按以下代码所示传入文件,但它不起作用。我尝试连接字符串仍然没有用
request.query(query + `IN ('${serverName.join("','")}')`)
const sql = require('mssql')
const config = require('../Util/config')
const fs = require('fs')
const log = require('../Util/log')
module.exports = function (req, res, viewFile, next) {
log(req)
const query = fs.readFileSync('./sql/Local-admin-user.sql', 'utf8')
// console.log(query);
const serverName = "'" + req.body.server_name.split(/\r?\n/).join("','") + "'"
console.log(serverName)
var dbConn = new sql.ConnectionPool(config)
dbConn.connect().then(function () {
var request = new sql.Request(dbConn)
request.query(query)
.then(output => {
let dataSet = output.recordset
console.log(dataSet)
dbConn.close()
})
.catch(err => {
let dataSet = err
console.log('error from routes.js', dataSet)
dbConn.close()
})
}).catch(function (err) {
console.log(err)
})
}
本地管理员-user.sql
select servername,
username,
groupname from GROUP_MEMBERS_DATA where servername in (@servers)
我收到类似
的错误
Incorrect syntax near servername
我是这样解决的
--remove "in (@servers)" from Local-admin-user.sql
--Example
select servername, username, groupname
from GROUP_MEMBERS_DATA where servername
在javascript中添加以下代码
const serverName = "'" + req.body.server_name.split(/\r?\n/).join("','") + "'"
const sqlquery = (query + ` IN (${serverName})`)
所以 sqlquery 的输出将如下所示
select servername, username, groupname
from GROUP_MEMBERS_DATA where servername in ('server1','server2')
我们如何将外部 sql 文件传递到 node-mssql 查询
我尝试按以下代码所示传入文件,但它不起作用。我尝试连接字符串仍然没有用
request.query(query + `IN ('${serverName.join("','")}')`)
const sql = require('mssql')
const config = require('../Util/config')
const fs = require('fs')
const log = require('../Util/log')
module.exports = function (req, res, viewFile, next) {
log(req)
const query = fs.readFileSync('./sql/Local-admin-user.sql', 'utf8')
// console.log(query);
const serverName = "'" + req.body.server_name.split(/\r?\n/).join("','") + "'"
console.log(serverName)
var dbConn = new sql.ConnectionPool(config)
dbConn.connect().then(function () {
var request = new sql.Request(dbConn)
request.query(query)
.then(output => {
let dataSet = output.recordset
console.log(dataSet)
dbConn.close()
})
.catch(err => {
let dataSet = err
console.log('error from routes.js', dataSet)
dbConn.close()
})
}).catch(function (err) {
console.log(err)
})
}
本地管理员-user.sql
select servername,
username,
groupname from GROUP_MEMBERS_DATA where servername in (@servers)
我收到类似
的错误Incorrect syntax near servername
我是这样解决的
--remove "in (@servers)" from Local-admin-user.sql
--Example
select servername, username, groupname
from GROUP_MEMBERS_DATA where servername
在javascript中添加以下代码
const serverName = "'" + req.body.server_name.split(/\r?\n/).join("','") + "'"
const sqlquery = (query + ` IN (${serverName})`)
所以 sqlquery 的输出将如下所示
select servername, username, groupname
from GROUP_MEMBERS_DATA where servername in ('server1','server2')