tsql clash:操作数类型冲突:日期与tinyint不兼容

时间:2017-04-05 00:13:49

标签: sql-server tsql

我有[A-NEST BY][A-CUT BY]字段都是datetime字段,为什么我在运行更新时会收到

  

消息206,级别16,状态2,过程NSTBY,第9行操作数类型   冲突:日期与tinyint不兼容

SET [A-NEST BY] = 
    case DateAdd( day, -10, t1.[A-CUT BY] )
         when 7 then DateAdd( day, -2, t1.[A-CUT BY] )
         when 1 then DateAdd( day, -3, t1.[A-CUT BY])
        else  t1.[A-CUT BY]-10
    end,

    FROM WORKORDERS T1
         INNER JOIN inserted i ON T1.[WORK ORDER #] = i.[WORK ORDER #]
        END

1 个答案:

答案 0 :(得分:0)

使您的查询工作的实际逻辑将是这样的:

UPDATE t1
SET [A-NEST BY] = 
    DATEADD
    (
        DAY, 
        -CASE DATEPART(WEEKDAY, t1.[A-CUT BY]) 
            WHEN 3 -- For Tuesday, ten days prior is a Saturday, so take an extra day off.
            THEN 11 
            WHEN 4 -- For Wednesday, ten days prior is a Sunday, so take an extra two days off.
            THEN 12 
            ELSE 10 -- Default to ten days.
            END,
        t1.[A-CUT BY]
    )
FROM WORKORDERS T1
INNER JOIN inserted i ON T1.[WORK ORDER #] = i.[WORK ORDER #]

那就是说,看起来你正在把它作为触发器。

如果这是,例如,你的WORKORDERS表上的插入触发器,我建议而不是使用触发器,你使用计算列(除非[A-NEST BY]日期可以更改)。