执行存储过程时参数未提供错误
Posted
技术标签:
【中文标题】执行存储过程时参数未提供错误【英文标题】:Paramater Not Supplied Error when Executing Stored Procedure 【发布时间】:2015-01-21 13:58:12 【问题描述】:我正在尝试通过 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
任何人都可以帮助解释为什么会抛出这个错误,因为我以前多次使用这种方法执行存储过程?
【问题讨论】:
【参考方案1】:你需要添加:
oCM.NamedParameters = True
在分配参数之前,否则它们实际上是按位置传递的。
【讨论】:
以上是关于执行存储过程时参数未提供错误的主要内容,如果未能解决你的问题,请参考以下文章