nodejs mssql - 传递的参数太多存储过程

nodejs mssql - Too many parameters passed Stored procedure

我 运行 进入 "Too many parameters passed" 到存储过程,同时尝试 运行 使用 NodeJs 中的存储过程mssql.

代码:

//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) 脚本 运行s 成功地具有以下代码行并且它可以工作。我想知道为什么我在 nodejs 中的代码会给我错误。

                      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 帮助 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 参数,它只是 returns 记录集,您可以通过函数回调获取相同的记录集:

   .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 类型,因此您可以简单地删除/注释掉下面的行:

.output('sqlOutput', sql.VarChar(1000), "Stored procedure has not run yet!!")