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

时间:2015-10-06 15:57:33

标签: sql-server vbscript ado

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

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 create parameter语句中的参数名称没有按名称链接到sproc中的变量。它似乎是基于顺序位置匹配,例如param 1匹配param 1,依此类推。如果我在中间某处留下一个,所有剩余的参数将被分配给sproc中的错误输入变量。

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

1 个答案:

答案 0 :(得分:0)

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

按名称传递参数所需的全部内容是将True属性添加到命令对象并将其设置为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 ,如下所示。添加了下面代码段中的第3行,否则它与原始代码相同:

curl -F name=khanhpn \
-F type_id=1 \
-X POST http://localhost:3000/api/v1/singers