SQL Management Studio - 不允许从数据类型datetime到numeric的隐式转换

时间:2014-06-17 12:34:58

标签: sql sql-server datetime dateadd

我只是尝试更新包含'datetype'列的表,但在使用dateadd函数时,我收到错误:

Implicit conversion from data type datetime to numeric is not allowed. Use the CONVERT function to run this query.

我在许多微软文档以及本网站上进行过搜索,但却无法解决错误持续存在的原因。它应该相当简单,但事实并非如此。这两个变量都是DECLARED作为日期时间,而简单的代码行就在if语句中的注释之后:

    DECLARE cur_hours_check CURSOR

FOR
    SELECT Period, Act_Batch_Time, Kg_Per_Hour, Total_QT_Produced, Total_Possible_KG,
            --The columns not affected but need for inserting the shadow day
           Division, Region, Business_Region, Plant, Work_Center, DaysInMonth, Total_Possible_Hours, Demonstrated_Capacity
    FROM zt_Demonstrated_Capacity_Trend
OPEN cur_hours_check

DECLARE             --Cursor Variables Pulling data
    @period                   datetime
,   @act_hours                float
,   @over_hours               float
,   @Total_QT_Produced        float 
,   @KG_per_hour              float
,   @Total_Possible_KG        float
--then the extra
,   @Division                 varchar(20)
,   @Region                   varchar(20)
,   @Business_Region          varchar(20)
,   @Plant                    varchar(40)
,   @Work_Center              varchar(40)
,   @DaysInMonth              int
,   @Total_Possible_Hours     int
,   @Demonstrated_Capacity    float

DECLARE             --Cursor Shadow variables:
    @shadow_period                 datetime 
,   @shadow_act_hours              float
,   @shadow_Total_QT_Produced      float
,   @shadow_Total_Possible_KG      float
,   @shadow_Percent_of_Total_Hours float

DECLARE             --Cursor adjusted first day variables
    @adj_act_hours                 float
,   @adj_Total_QT_Produced         float
,   @adj_Total_Possible_KG         float
,   @adj_Percent_of_Total_Hours    float

FETCH NEXT FROM cur_hours_check
INTO @period, @act_hours, @KG_per_hour, @Total_QT_Produced, @Total_Possible_KG
   ,@Division, @Region, @Business_Region, @Plant, @Work_Center, @DaysInMonth, @Total_Possible_Hours, @Demonstrated_Capacity

WHILE @@FETCH_STATUS = 0
BEGIN
IF @act_hours > 24.0
BEGIN
-- Setting the shadow period to carry over values relative to 24 hour time window
    SET @shadow_period = dateadd(d, 1, @period)
    SET @shadow_act_hours = @act_hours - 24.0
    SET @shadow_Total_QT_Produced = @Total_QT_Produced - (@KG_per_hour * @shadow_act_hours)
    SET @shadow_Percent_of_Total_Hours = (@shadow_act_hours/24.0)*100

2 个答案:

答案 0 :(得分:3)

你的FETCH即将结束。你有错误的顺序;所以它试图将日期时间放入非日期时间变量中。我也测试了你的dateadd,没关系。

仅供参考:您可以双击错误以确切了解问题所在。

答案 1 :(得分:0)

如果您想在@period添加一天,请执行以下操作:

SET @shadow_period = DATEADD(day, 1, @period)