2

I am trying to run node-mssql query, if I run simple quires it is getting executed. But when I use OPENROWSET and Microsoft.ACE.OLEDB.12.0, it is showing some errors.

Here is server.js code

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..');});`

and when i hit node server.js on command prompt I am getting following errors on command prompt:

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

The same query is getting executed in SQL Server Management Studio , it is successfully inserting excel data to database.Excel sheet data looks like this:

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

Here is the plunker link

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
naik3
  • 319
  • 2
  • 8
  • 22
  • Could you check, is there any syntax error in the end of the line `1540_OPENROWSET_Examples.xls;,\''`. I mean the comma is need to place after the `\''` ? – Arulkumar May 09 '16 at 07:32
  • Hi , thank you for reply . I tried as u said , still it is showing – naik3 May 09 '16 at 07:42
  • `{ [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 ','.] name: 'RequestError', message: 'Incorrect syntax near \',\'.', code: 'EREQUEST', number: 102, lineNumber: 1, state: 1, class: 15, serverName: 'localhost\\SQLEXPRESS', procName: '' } ] }` – naik3 May 09 '16 at 07:42

1 Answers1

1

I see few syntax errors in your code:

Sample code for OPENROWSET is:

FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=D:\testing.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')
  1. In your code extra ' between the Excel and Database keywords ,\'Excel 12.0\';\'Database=D:\\sample ..., that need to correct

  2. The SELECT SalesPersonID, TerritoryID FROM [SELECT_Example$] need ' before and after.

So your working code will be:

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)

Update :

The OP received this configuration error:

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

To fix the configuration error, the below script need to execute:

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
Arulkumar
  • 12,966
  • 14
  • 47
  • 68
  • But it is giving some configuration error : The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" reported an error. – naik3 May 09 '16 at 09:46
  • Could you execute the below query `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 ` after execute your code – Arulkumar May 09 '16 at 10:26
  • Hi ... I am facing one more problem. Not any errors , data from excel sheet is storing properly on to sql server database , but when I press refresh button or if I go to that link again in my application , data is repeating in the database. Means again it is retrieving values from excel and storing same data again in the database . How can I avoid data repetition. Can you please help me to solve this issue. – naik3 May 10 '16 at 12:48
  • @naik3 : I don't have much idea about your requirement. Can you post a new question about that (_you can link this post_), so some other can help you. – Arulkumar May 10 '16 at 13:28
  • Thank you for the suggestion. I will post another question regarding this issue. – naik3 May 11 '16 at 06:27