再会,
我正在尝试从 SQL 导出一些数据,将其存储起来,然后将该数据与同一查询的结果进行比较。我能看到的最简单的方法是使用 TVP,但它似乎不适用于 node-mssql 并且它没有给我一个 TVP 相关的错误。
我运行代码:
var _sql = require('mssql');
var _conn = new _sql.Connection(/*CONN STR*/,function(err) {
if (err) {
console.log('ERROR Unable to connect to DB:',err);
} else {
var oInitRequest = new _sql.Request(_conn);
oInitRequest.query(/*DB QUERY*/)
.then(function(oInitRS) {
console.log('oInitData:',oInitRS);
var oInitTable = oInitRS.toTable();
console.log('oInitTable:',oInitTable);
var oCheckRequest = new _sql.Request(_conn);
oCheckRequest.input('oInitTable',_sql.TVP,oInitTable);
oCheckRequest.query('SELECT * FROM @oInitTable')
.then(function(oCheckRS) {
console.log('oInitTable re-read:',oCheckRS);
})
.catch(function(err) {
console.log('ERROR unable to pass oInitTable to new query',err);
});
})
.catch(function(err) {
console.log('ERROR Unable to retrieve oInitData',err);
});
}
});
我收到错误:
ERROR unable to pass oInitTable to new query
{
[RequestError: Could not find stored procedure 'sp_executesql'.]
name: 'RequestError',
message: 'Could not find stored procedure \'sp_executesql\'.',
code: 'EREQUEST',
number: 2812,
lineNumber: 1,
state: 1,
class: 16,
serverName: /*REDACTED*/,
procName: '',
precedingErrors: []
}
为什么第一个查询可以使用 sp_executesql 成功运行查询,但第二个查询不存在存储过程?
我尝试使用准备好的语句,但即使是没有参数的标准语句也无法说明错误:
{
[RequestError: Could not find prepared statement with handle 0.]
name: 'RequestError',
message: 'Could not find prepared statement with handle 0.',
code: 'EREQUEST',
number: 8179,
lineNumber: 1,
state: 4,
class: 16,
serverName: /*REDACTED*/,
procName: 'sp_execute',
precedingErrors: []
}
有谁知道如何让查询接受 TVP 而无需专门为 1 个查询创建存储过程?