在尝试使用 mssql 在 NodeJs 中运行存储过程时,我遇到了“传递给存储过程的参数过多”的情况。
代码:
//This computerName is what we'll find in our mssql server to see
//if the server entry exist or not and the stored procedure will take this as a parameter.
var computerName = "some.fake.server.com";
var secProfile = "";
//Logic
// If computerName passed is valid and not null.
//if (computerName != "") {
var sql = require('mssql');
var config = {
user: 'dbuser',
password: 'secure9ass',
server: 'dbserver.domain.com',
database: 'DBName',
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
}
}
sql.connect(config).then(function(output) {
// Stored Procedure
new sql.Request()
.input("ComputerName", sql.VarChar(100), computerName)
.output('sqlOutput', sql.VarChar(1000), "Stored procedure has not run yet!!")
.execute('dbo.getSysStatus_ByName').then(function(recordsets) {
console.dir(recordsets);
}).catch(function(err) {
// ... error checks
console.log('ERROR1::: ' + err)
console.log("----")
console.log(err)
console.log("====")
console.log(recordsets)
console.log("----")
console.log('ERROR2::: '+ sqlOutput);
console.log('ERROR3::: '+ request.parameters.sqlOutput.value);
});
console.log(output);
}).catch(function(err) {
// ... error checks
console.log('ERROR5::: '+ err);
});
错误:传递给过程名称的参数过多:getSysStatus_ByName
我在数据库中检查了存储过程只有一个它所期望的参数,该参数是:ComputerName
我知道我只是缺少参数名称,但我已经尝试过
.input("@ComputerName", sql.VarChar(100), computerName)
或
.input("computerName", sql.VarChar(100), computerName)
或
.input("computername", sql.VarChar(100), computerName)
没有任何效果,并且给了我同样的错误。另外,尝试将参数类型从 sql.VarChar(xxx) 更改为 sql.Int (在这种情况下,它错误地指出无效类型,所以我知道 sql.VarChar(xxx) 很好。
成功运行的 .vb(Visual Basic)脚本之一具有以下代码行并且可以运行。我想知道为什么我在 Node.js 中的代码给出了错误。
Set objCmd = CreateObject("ADODB.Command")
ObjCmd.ActiveConnection = Conn
ObjCmd.CommandTimeout = 180 'in seconds
ObjCmd.CommandType = 4 'Stored Procedure
ObjCmd.CommandText = "dbo.getSysStatus_ByName"
objCmd.Parameters.Append objCmd.CreateParameter("@ComputerName", 200, 1, 1024, ComputerName)
根据 CreateParameter(ADO help page ),它说,200 是 #
adVarChar 200 A string value (Parameter object only).
1 means: direction variable (where 1 is for an Input Parameter in my case) and
1024 is the size of the input variable.
我没有可以尝试的 VPN 连接,但我希望不会由于 1024 与 1000 大小而出现错误(在我的 .input(..) 行代码示例中)。明天我会测试一下。
最佳答案
如果您在过程定义中为参数指定 OUTPUT 关键字,则只有您有权使用以下行;当您定义 sql.Request()
.output('sqlOutput', sql.VarChar(1000), "Stored procedure has not run yet!!")
在存储过程中使用 OUTPUT
类型只是考虑您的情况的示例:
CREATE PROCEDURE dbo.getSysStatus_ByName
@ComputerName varchar(100),
@sqlOutput VarChar(1000) OUTPUT
AS
BEGIN
//YOUR SP CODE HERE
END
如果您的存储过程没有 OUTPUT
参数,并且它只是返回记录集,您可以通过函数回调获取相同的记录集:
.execute('dbo.getSysStatus_ByName').then(function(recordsets) {
//recordsets is an result return by your executed stored procedure })
SQL Server with NODE.JS - Get started
就像您的情况一样,您的存储过程不包含任何 Output
类型,因此您只需删除以下行的/comment 即可:
.output('sqlOutput', sql.VarChar(1000), "Stored procedure has not run yet!!")
关于sql - nodejs mssql - 传递的参数太多存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876906/