我正在尝试运行 node-mssql 查询,如果我运行简单的查询,它将被执行。但是当我使用OPENROWSET
and时Microsoft.ACE.OLEDB.12.0
,它显示了一些错误。
这是server.js代码
var express = require('express');
var app = express();
app.get('/', function (req, res) {
var sql = require("mssql");
// config for your database
var config = {
user: '..',
password: '....',
server: 'localhost\\SQLEXPRESS',
database: 'test_databasae'
};
// connect to your database
sql.connect(config, function (err) {
if (err)
console.log(err);
else
console.log("Connection successful");
// create Request object
var request = new sql.Request();
// query to the database and get the records
/*request.query('select * from table1', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});*/
request.query('INSERT INTO [mytable](SalesPersonID,TerritoryID)' +
'SELECT SalesPersonID,TerritoryID FROM OPENROWSET(' +
'\'Microsoft.ACE.OLEDB.12.0\',\'Excel 12.0\';\'Database=D:\\sample\\test\\data\\1540_OPENROWSET_Examples.xls;,\'' +
'SELECT SalesPersonID, TerritoryID FROM [SELECT_Example$])',function(err,recordset){
if(err) console.log(err)
console.log("success");
});
});});var server = app.listen(5000, function () {
console.log('Server is running..');});`
当我在命令提示符下点击node server.js时,我在命令提示符下收到以下错误:
Server is running..
Connection successful
{ [RequestError: Incorrect syntax near ')'.]
name: 'RequestError',
message: 'Incorrect syntax near \')\'.',
code: 'EREQUEST',
number: 102,
lineNumber: 1,
state: 1,
class: 15,
serverName: 'localhost\\SQLEXPRESS',
procName: '',
precedingErrors:
[ { [RequestError: Incorrect syntax near the keyword 'SELECT'.]
name: 'RequestError',
message: 'Incorrect syntax near the keyword \'SELECT\'.',
code: 'EREQUEST',
number: 156,
lineNumber: 1,
state: 1,
class: 15,
serverName: 'localhost\\SQLEXPRESS',
procName: '' } ] }
success
在 SQL Server Management Studio 中执行相同的查询,它成功地将 excel 数据插入数据库。Excel 工作表数据如下所示:
SalesPersonID TerritoryID
-------- -----------
275 2
276 4
277 3