执行SQL存储过程时转换DateTime错误

时间:2011-08-17 12:59:22

标签: sql-server-2008 stored-procedures datetime-format

我写了以下存储过程:

GO
/****** Object:  StoredProcedure [dbo].[ReadCounters]    Script Date: 08/17/2011 13:43:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- ReadCounters --
ALTER PROCEDURE [dbo].[ReadCounters]
    @type bit,
    @startDate DateTime,
    @endDate DateTime
AS
BEGIN
    DECLARE
    @AllCounterIds NVARCHAR(MAX),
    @Query NVARCHAR(MAX);

    SELECT @AllCounterIds = STUFF((
            SELECT DISTINCT '],[' + CAST([CounterId] AS VARCHAR(32))
            FROM AllCounters
            WHERE [DateTime] > @startDate AND [DateTime] < @endDate AND [Type] = @type
            for xml path('')), 1, 2, '') + ']';

    SET @Query = 'SELECT [DateTime], pvt.* from
                    (SELECT [Type], [DateTime], [Value], [CounterId]
                     FROM AllCounters WHERE CounterId IN 
                        (
                         SELECT DISTINCT([CounterId]) 
                         FROM AllCounters
                         WHERE [DateTime] > '''+ @startDate +''' AND [DateTime] < '''+ @endDate +''' AND [Type] = '+ @type +'
                        ) AND [DateTime] > '''+ @startDate +''' AND [DateTime] < '''+ @endDate +''' AND [Type] = '+ @type +'
                    ) S
                PIVOT
                (
                    SUM (Value)
                    FOR CounterId IN
                    (' + @AllCounterIds + ')
                ) AS pvt;';         
    EXECUTE(@Query);
END

现在,当我尝试使用以下任何一种方式执行此SP时:

exec ReadCounters 1,'2013-10-05', '2011-11-30'
exec ReadCounters 1,'2013-10-05 00:00:00', '2011-11-30 00:00:00'
exec ReadCounters 1,'2013-10-05 00:00:00.000', '2011-11-30 00:00:00.000'
exec ReadCounters 1,{ts '2013-10-05 00:00:00.000'}, {ts '2011-11-30 00:00:00.000'}

我收到以下错误:

Msg 241, Level 16, State 1, Procedure ReadCounters, Line 19
Conversion failed when converting date and/or time from character string.

任何建议为什么给我错误。如果只执行Select查询,它运行得非常好。

2 个答案:

答案 0 :(得分:3)

您当然需要CONVERT格式化

....
WHERE [DateTime] > '''+ CONVERT(varchar(30), @startDate, 120) +''' AND ...
...

为什么SQL服务器猜测要连接不同的数据类型?

错误是因为NVARCHAR(MAX)的优先级低于these rules

的日期时间

答案 1 :(得分:2)

我宁愿这样做 - 保存了很多混乱的转换和红/黑休息(虽然我仍然建议将逗号分隔的ID列表连接起来):

SET @Query = N'SELECT [DateTime], pvt.* from
            (SELECT [Type], [DateTime], [Value], [CounterId]
             FROM AllCounters WHERE CounterId IN 
                (
                 SELECT DISTINCT([CounterId]) 
                 FROM AllCounters
                 WHERE [DateTime] > @startDate AND [DateTime] < @endDate AND [Type] = @type 
                ) AND [DateTime] > @startDate AND [DateTime] < @endDate AND [Type] = @type 
            ) S
            PIVOT
            (
                SUM (Value)
                FOR CounterId IN
                (' + @AllCounterIds + ')
            ) AS pvt;';         

EXEC sp_executesql @query, 
    N'@startDate DATETIME, @endDate DATETIME, @type BIT',
    @startDate, @endDate, @type;