我只是试图更新包含'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
你可以发表一些更多的表格设计和你正在使用atm的代码吗?变量的声明在哪里完成?显然有些东西不是'DATETIME'类型.. :) – NickyvV
我更新了代码 – Sauron
您是否还有表'zt_Demonstrated_Capacity_Trend'的DDL?这可能是其中的一个列没有正确定义。你有使用'FLOAT'的具体原因吗?因为浮点数据是近似的;因此,数据类型范围中的所有值都不能完全表示。 – NickyvV