gpt4 book ai didi

sql-server - 运行 node-mssql 查询时出错

转载 作者:太空宇宙 更新时间:2023-11-03 22:12:47 25 4
gpt4 key购买 nike

我正在尝试运行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 数据插入数据库。Excel 工作表数据如下所示:

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

关于sql-server - 运行 node-mssql 查询时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37109826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com