2014-06-17 70 views
0

我只是试图更新包含'datetype'列的表,但在使用dateadd函数时,我收到以下错误:SQL Management Studio - 不允许从数据类型datetime到数字的隐式转换

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

我已经在许多微软文档以及在这个网站上搜索,但不能承认为什么错误持续存在。它应该相当简单,但事实并非如此。这两个变量声明为日期时间和简单的代码行是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 
+1

你可以发表一些更多的表格设计和你正在使用atm的代码吗?变量的声明在哪里完成?显然有些东西不是'DATETIME'类型.. :) – NickyvV

+0

我更新了代码 – Sauron

+0

您是否还有表'zt_Demonstrated_Capacity_Trend'的DDL?这可能是其中的一个列没有正确定义。你有使用'FLOAT'的具体原因吗?因为浮点数据是近似的;因此,数据类型范围中的所有值都不能完全表示。 – NickyvV

回答

3

这是你的FETCH正在经历。你有错误的顺序;所以它试图将日期时间放入非日期时间变量中。此外,我已经测试了你的dateadd,它很好。

供参考:您可以双击错误以确切地查看问题的位置。

+0

在我阅读关于该问题的评论之前,我正要将其标记为不是答案。您可能想要编辑您的答案,因为它不再给您提供OP要求澄清的印象。 – hvd

+0

公平点..完成! –

0

如果你想添加一天@period做到以下几点:

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

对不起,我编辑了代码,因为我发现了更新后的文件,但它仍然不起作用 – Sauron

相关问题