使用 VBScript 在 ADO 的命令对象中仅指定某些参数?

Only Specify Certain Parameters in Command Objects in ADO with VBScript?

如果我在 SQL 服务器中有一个存储过程,其中包含许多命名输入参数,所有参数的默认值如下:

CREATE PROCEDURE [dbo].[proc_name] 
(   
    @IN_mode CHAR(1) = NULL, /* 'I','U'*/
    @IN_external_identity_id INT = -1,
    @IN_provider_name VARCHAR(45) = NULL,
    @IN_login_id VARCHAR(255) = NULL,
    @IN_email VARCHAR(255) = NULL,
    @IN_ip VARCHAR(45) = NULL,
    @IN_first_name VARCHAR(255) = NULL,
    @IN_last_name VARCHAR(255) = NULL,
    @IN_json_response_raw VARCHAR(8000) = NULL,
    @IN_user_id VARCHAR(255) = NULL,
    @IN_name VARCHAR(255) = NULL
)
AS 
    BEGIN
...

...在 vbScript 中,我这样调用程序:

Set oSproc = CreateObject("ADODB.Command") 
With oSproc
    .ActiveConnection = oConn
    .CommandText = "bfsp_insert_NEW_EXTERNAL_IDENTITY"
    .CommandType = adCmdStoredProc 
    With .Parameters
        .Append oSproc.CreateParameter("@IN_mode", adChar, adParamInput, 1, "I")
        .Append oSproc.CreateParameter("@IN_external_identity_id", adInteger, adParamInput, 0, -1) '-1 because field isn't needed on new insert
        .Append oSproc.CreateParameter("@IN_provider_name", adVarChar, adParamInput, 45, "FACEBOOK")
        .Append oSproc.CreateParameter("@IN_login_id", adVarChar, adParamInput, 255, getInput("fbNewUserName"))
        .Append oSproc.CreateParameter("@IN_email", adVarChar, adParamInput, 255, oFbUser.email)
        .Append oSproc.CreateParameter("@IN_ip", adVarChar, adParamInput, 46, getUserIp())
        .Append oSproc.CreateParameter("@IN_first_name", adVarChar, adParamInput, 255, oFbUser.first_name)
        .Append oSproc.CreateParameter("@IN_last_name", adVarChar, adParamInput, 255, oFbUser.last_name)
        .Append oSproc.CreateParameter("@IN_json_response_raw", adVarChar, adParamInput, 8000, oFbUser.rawJson)
        .Append oSproc.CreateParameter("@IN_user_id", adVarChar, adParamInput, 255, oFbUser.id)
        .Append oSproc.CreateParameter("@IN_name", adVarChar, adParamInput, 255, oFbUser.name)
    End With 
    .Execute 
End With 
iNewExternalIdentityId = oSproc.parameters("@OUTPUT_originalId").Value 'grabbing the new EXTERNAL_IDENTITY.ID 
Set oSproc = Nothing

我注意到 vbscript 创建参数语句中的参数名称并没有 link 存储过程中变量的名称。它似乎是根据序号位置匹配的,例如参数 1 匹配参数 1,依此类推。如果我遗漏了一个,比如说,在中间某处,所有剩余的参数将被分配给 sproc 中错误的输入变量。

如何在 vbscript 代码中创建参数,以便我可以只发送我需要的 select 参数,并且仍然使它们与过程中的参数正确匹配?

我发布了一个包含@jeroen-mostert 建议的正式答案。

按名称传递参数所需要做的就是将 namedParameters 属性 添加到命令对象并将其设置为 True,如下所示。添加了下面代码段中的第 3 行,否则与原始代码相同:

Set oSproc = CreateObject("ADODB.Command") 
With oSproc
    .NamedParameters = True
    .ActiveConnection = oConn
    .CommandText = "bfsp_insert_NEW_EXTERNAL_IDENTITY"
    .CommandType = adCmdStoredProc 
    With .Parameters
        .Append oSproc.CreateParameter("@IN_mode", adChar, adParamInput, 1, "I")
        .Append oSproc.CreateParameter("@IN_external_identity_id", adInteger, adParamInput, 0, -1) '-1 because field isn't needed on new insert
        .Append oSproc.CreateParameter("@IN_provider_name", adVarChar, adParamInput, 45, "FACEBOOK")
        .Append oSproc.CreateParameter("@IN_login_id", adVarChar, adParamInput, 255, getInput("fbNewUserName"))
        .Append oSproc.CreateParameter("@IN_email", adVarChar, adParamInput, 255, oFbUser.email)
        .Append oSproc.CreateParameter("@IN_ip", adVarChar, adParamInput, 46, getUserIp())
        .Append oSproc.CreateParameter("@IN_first_name", adVarChar, adParamInput, 255, oFbUser.first_name)
        .Append oSproc.CreateParameter("@IN_last_name", adVarChar, adParamInput, 255, oFbUser.last_name)
        .Append oSproc.CreateParameter("@IN_json_response_raw", adVarChar, adParamInput, 8000, oFbUser.rawJson)
        .Append oSproc.CreateParameter("@IN_user_id", adVarChar, adParamInput, 255, oFbUser.id)
        .Append oSproc.CreateParameter("@IN_name", adVarChar, adParamInput, 255, oFbUser.name)
    End With 
    .Execute 
End With 
iNewExternalIdentityId = oSproc.parameters("@OUTPUT_originalId").Value 'grabbing the new EXTERNAL_IDENTITY.ID 
Set oSproc = Nothing