运行node-mssql查询时出错

时间:2016-05-09 07:19:19

标签: sql-server node.js excel openrowset node-mssql

我正在尝试运行node-mssql查询,如果我运行简单的quires就会执行它。但是当我使用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..');});`

当我在命令提示符下点击节点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

Here is the plunker link

1 个答案:

答案 0 :(得分:1)

我在您的代码中看到很少的语法错误:

OPENROWSET的示例代码为:

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

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

  3. 所以你的工作代码将是:

    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收到此配置错误:

      

    OLE DB提供程序" Microsoft.ACE.OLEDB.12.0"对于链接服务器"(null)"报告错误。

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

    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