2015-10-06 60 views
1

如果我在SQL Server中的存儲過程與多個名爲輸入參數,全部用默認值如下:只用VBScript在ADO的命令對象中指定某些參數?

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創建參數語句中的參數名稱並不按名稱鏈接到存儲區中的變量。它似乎根據序號位置進行匹配,例如,參數1匹配參數1,依此類推。如果我把它留在某處,那麼所有剩下的參數將被分配給存儲區中錯誤的輸入變量。

如何在vbscript代碼中創建參數,以便只發送我需要的選擇參數,並仍然使它們與過程中的參數正確匹配?

+1

嘗試'.NamedParameters = TRUE',每https://msdn.microsoft.com/library/ms675840(聲明:不測試)。 –

+0

否則,另一種方法是將參數添加爲無值(或爲空)而不是省略參數。 – tgolisch

+0

@ jeroen-mostert - 工作...我會用你的建議創建一個正式的答案 – GWR

回答

0

我發佈了一個正式的答案,包含來自@ 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