执行存储过程时出现错误 "Incorrect syntax near '-' "
Getting error "Incorrect syntax near '-' " while executing Stored Procedure
我已经使用两个输入参数和两个输出参数成功创建了一个存储过程。当我执行此过程时,出现错误
Incorrect syntax near '-'
靠近第一个参数。
ALTER PROCEDURE [dbo].[Agent_interactions_report]
(
@StartDate date,
@EndDate date,
@ERRORCODE AS INT OUTPUT,
@ERRORDESCRIPTION AS VARCHAR(4000) OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
begin Try
select
cast([CallStartTime] as Date) as Date,
[AgentID] as [Agent ID], [Agent_Name] as [Agent name],
[CustomerID] as [Cust-ID], [Account_Number] as [Account number],
[Transaction_Des] as [Transaction],
CallStartTime AS [Call start time],
CallEndTime AS [Call end time], CallID as [Call ID]
from
[TBL_AGENT_TRANSACTIONS]
where
cast(CallStartTime as DATE) >= @StartDate
and cast(CallEndTime as Date) <= @EndDate
end Try
begin catch
SET @ERRORCODE = ERROR_NUMBER()
SET @ERRORDESCRIPTION = ERROR_MESSAGE()
end catch
END
这是执行结果:
DECLARE @return_value int,
@ERRORCODE int,
@ERRORDESCRIPTION varchar(4000)
EXEC @return_value = [dbo].[Agent_interactions_report]
@StartDate = 2015-04-27,
@EndDate = 2015-04-27,
@ERRORCODE = @ERRORCODE OUTPUT,
@ERRORDESCRIPTION = @ERRORDESCRIPTION OUTPUT
SELECT @ERRORCODE as N'@ERRORCODE',
@ERRORDESCRIPTION as N'@ERRORDESCRIPTION'
我在“@StartDate = 2015-04-27”附近遇到错误,但是当我通过将此输入提供给输入参数手动执行 SP 时,我得到了预期的结果。
注:
由于我的声誉低于 10,所以我不应该上传屏幕截图,这对于理解问题更有用。如果您需要更多信息,请告诉我。
以这种方式处理日期时,您需要将它们用引号括起来!它无法识别您所拥有的数据类型并认为您发送的是字符串。
试试用引号括起日期?
@StartDate = '2015-04-27',
@EndDate = '2015-04-27',
我已经使用两个输入参数和两个输出参数成功创建了一个存储过程。当我执行此过程时,出现错误
Incorrect syntax near '-'
靠近第一个参数。
ALTER PROCEDURE [dbo].[Agent_interactions_report]
(
@StartDate date,
@EndDate date,
@ERRORCODE AS INT OUTPUT,
@ERRORDESCRIPTION AS VARCHAR(4000) OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
begin Try
select
cast([CallStartTime] as Date) as Date,
[AgentID] as [Agent ID], [Agent_Name] as [Agent name],
[CustomerID] as [Cust-ID], [Account_Number] as [Account number],
[Transaction_Des] as [Transaction],
CallStartTime AS [Call start time],
CallEndTime AS [Call end time], CallID as [Call ID]
from
[TBL_AGENT_TRANSACTIONS]
where
cast(CallStartTime as DATE) >= @StartDate
and cast(CallEndTime as Date) <= @EndDate
end Try
begin catch
SET @ERRORCODE = ERROR_NUMBER()
SET @ERRORDESCRIPTION = ERROR_MESSAGE()
end catch
END
这是执行结果:
DECLARE @return_value int,
@ERRORCODE int,
@ERRORDESCRIPTION varchar(4000)
EXEC @return_value = [dbo].[Agent_interactions_report]
@StartDate = 2015-04-27,
@EndDate = 2015-04-27,
@ERRORCODE = @ERRORCODE OUTPUT,
@ERRORDESCRIPTION = @ERRORDESCRIPTION OUTPUT
SELECT @ERRORCODE as N'@ERRORCODE',
@ERRORDESCRIPTION as N'@ERRORDESCRIPTION'
我在“@StartDate = 2015-04-27”附近遇到错误,但是当我通过将此输入提供给输入参数手动执行 SP 时,我得到了预期的结果。
注:
由于我的声誉低于 10,所以我不应该上传屏幕截图,这对于理解问题更有用。如果您需要更多信息,请告诉我。
以这种方式处理日期时,您需要将它们用引号括起来!它无法识别您所拥有的数据类型并认为您发送的是字符串。
试试用引号括起日期?
@StartDate = '2015-04-27',
@EndDate = '2015-04-27',