执行存储过程时未提供参数错误

Paramater Not Supplied Error when Executing Stored Procedure

我正在尝试通过 Excel 2007 VBA 使用 ADO 执行存储过程 (SQL Server 2008)。执行存储过程时,我收到以下错误:Procedure or function 'crl_GetData' expects parameter '@StartDate', which was not supplied.

我使用 ADODB.Command 方法提供参数,如下所示:

Public Function fGetMI(strStart As String, strEnd As String) As ADODB.Recordset

Dim oDB As ADODB.Connection: Set oDB = New ADODB.Connection
Dim oCM As ADODB.Command: Set oCM = New ADODB.Command
Dim oRS As ADODB.Recordset

oDB.Open gcConn

With oCM
    .ActiveConnection = oDB
    .CommandType = adCmdStoredProc
    .CommandText = "crl_GetData"
    .Parameters.Append .CreateParameter("@StartDate", adDate, adParamInput, , strStart)
    .Parameters.Append .CreateParameter("@EndDate", adDate, adParamInput, , strEnd)
    Set oRS = .Execute 'Error thrown here'
End With

If Not oRS.BOF And Not oRS.EOF Then
    Set fGetMI = oRS
End If

oRS.Close
Set oRS = Nothing
oDB.Close
Set oDB = Nothing

End Function

Set oRS = .Execute 行收到错误。

存储过程需要 @StartDate 参数和 @EndDate 参数,存储过程定义如下:

CREATE PROCEDURE [dbo].[crl_GetData]
    @TL varchar(128) = null,
    @Unit varchar(16) = null,
    @Site varchar(16) = null,
    @OutcomeId int = null,
    @Auth varchar(128) = null,
    @StartDate datetime,
    @EndDate datetime
AS
    BEGIN
        SELECT
            [Claim Handler],
            [Team Leader],
            [Unit],
            [Site],
            [Claim Reference],
            [Outcome],
            [Authoriser],
            [Date]

        FROM
            crl_all_data

        WHERE
            [Date] BETWEEN @StartDate AND @EndDate
            AND (@TL IS NULL OR ([TL_ID] = @TL))
            AND (@Unit IS NULL OR ([Unit] = @Unit))
            AND (@Site IS NULL OR ([Site] = @Site))
            AND (@OutcomeId IS NULL OR ([OUTCOME_ID] = @OutcomeId))
            AND (@Auth IS NULL OR ([AUTH_ID] = @Auth))

        ORDER BY
            [Date] ASC

        OPTION(RECOMPILE);
    END

GO

任何人都可以帮助解释为什么会抛出此错误,因为我之前多次使用此方法执行存储过程?

您需要添加:

oCM.NamedParameters = True

在分配参数之前,否则它们实际上是按位置传递的。