运行 node-mssql 查询时出错

Error while running node-mssql query

我正在尝试 运行 node-mssql 查询,如果我 运行 简单查询它正在执行。但是当我使用 OPENROWSETMicrosoft.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 数据插入到 database.Excel sheet 数据中,如下所示:

SalesPersonID   TerritoryID
--------        -----------
275              2
276              4
277              3

Here is the plunker link

我在您的代码中发现了一些语法错误:

OPENROWSET 的示例代码是:

FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=D:\testing.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')
  1. 在您的代码中 ExcelDatabase 关键字 ,\'Excel 12.0\';\'Database=D:\sample ... 之间的额外 ',需要更正

  2. SELECT SalesPersonID, TerritoryID FROM [SELECT_Example$]前后需要'

因此您的工作代码将是:

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;HDR=YES\', ' + 
                '\'SELECT SalesPersonID, TerritoryID FROM [SELECT_Example$]\')',function(err,recordset){
    if(err) console.log(err)

更新:

OP 收到此配置错误:

The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" reported an error.

要修复配置错误,需要执行以下脚本:

USE [master] 
GO 

EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1 
GO 

EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1 
GO