【发布时间】:2020-12-24 21:07:55
【问题描述】:
我正在尝试从我的节点应用程序中的 SQL 调用已保存的存储过程。我的服务器已连接,我可以毫无问题地执行我的 selectRandom5 保存的过程。
我遇到的问题是当我尝试执行 getById 时,我需要声明 @Id 输入。我已经尝试了该函数的几个变体,但没有成功,这里有两个我尝试过。
我收到的错误消息是 UnhandledPromiseRejectionWarning: RequestError: Incorrect syntax near '?'。
selectById(req, res) {
var theId = req.params.id;
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query("CALL Addresses_SelectById(?)", [theId], function (err, recordset) {
if (err) console.log("connect", err);
// send records as a response
res.send(recordset);
console.log(recordset);
});
});
}
然后我尝试了另一个函数,我从中得到的错误消息是'必须声明标量变量“@Id”。'
selectById(req, res) {
var theId = req.params.id;
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query(`SET @Id = ${theId}CALL Addresses_SelectById(@Id)`, function (err, recordset) {
if (err) console.log("connect", err);
// send records as a response
res.send(recordset);
console.log(recordset);
});
});
}
我只是希望能够将参数传递给 SQL 以便能够创建更新或获取,但到目前为止我还没有找到传递参数的正确方法。
任何帮助将不胜感激!谢谢大家
【问题讨论】:
标签: mysql node.js api express stored-procedures