通过SQL Smalldatetime时带有单引号的环绕变量

时间:2010-01-20 21:55:37

标签: sql tsql

我有一个问题/问题,我今天可能已经盯着太久了。

我有一个从Web应用程序接收数据的存储过程。数据以小时间格式输入。我试图将此信息传递给第二个存储过程,但第二个存储过程将不会触发,除非数据是单引号。将它转换为varchar会更好吗?

由于显而易见的原因,SET @CompletedDate必须是'2010-01-20 15:28:00'。如何将此信息传递给第二个程序?

DECLARE @return_value int
    ,@TaskID int
    ,@CompletedDate smalldatetime
SET @TaskID = 90
SET @CompletedDate = 2010-01-20 15:28:00

EXEC    @return_value = [dbo].[usp_Task_Completion]
        @TaskID = @TaskID,
        @CompletedDate = @CompletedDate

这是usp_Task_Completion SP

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[usp_Task_Completion]
@TaskID int 
,@CompletedDate smalldatetime
AS
BEGIN

SET NOCOUNT ON;

--Mark Transaction as complete
UPDATE dbo.Task
SET Completed = 1
    ,CompletedDate =  @CompletedDate
WHERE TaskID = @TaskID

5 个答案:

答案 0 :(得分:2)

不,因为varchar也必须用单引号括起来,只需在它周围加上引号

DECLARE @return_value int
    ,@TaskID int
    ,@CompletedDate smalldatetime
SET @TaskID = 90
SET @CompletedDate = '2010-01-20 15:28:00'

EXEC    @return_value = [dbo].[usp_Task_Completion]
        @TaskID = @TaskID,
        @CompletedDate = @CompletedDate

答案 1 :(得分:0)

是的,您必须将小日期时间2010-01-20 15:28:00附加到单引号中 - > '2010-01-20 15:28:00'

答案 2 :(得分:0)

你可能在@CompletedDate上有一个命名混淆。更改声明中的变量名称,然后将set命令更改为使用单引号,如下所示:

SET @CompletedDateNewName = '2010-01-20 15:28:00'

@CompletedDate仍然可以是smalldatetime类型

答案 3 :(得分:0)

这有点评论。有什么问题?以下是我的测试:

/*
create proc usp_Task_Completion (
    @TaskID int,
    @CompletedDate smalldatetime)
as 

    return 1000
*/
go


DECLARE @return_value int
    ,@TaskID int
    ,@CompletedDate smalldatetime
SET @TaskID = 90

/* This works */
--SET @CompletedDate = '2010-01-20 15:28:00'    

/* This doesn't work (incorrect syntax near '15'. */
--SET @CompletedDate = 2010-01-20 15:28:00      

/* This runs but yields unintended results (2010-01-20 = 1989; convert to datetime = '1905-06-13' */
SET @CompletedDate = 2010-01-20 

EXEC    @return_value = [dbo].[usp_Task_Completion]
        @TaskID = @TaskID,
        @CompletedDate = @CompletedDate

select @return_value

答案 4 :(得分:0)

我猜测CompletedDate列不是smalldatetime,并且它试图将该列中的每个值转换为smalldatetime类型(有些失败)。请检查列类型是否确定。