我已经使用 CASE 重写了您的声明(但我意识到 IIF 在 2012 年有效):
UPDATE EMP
SET fte_adj =
CASE WHEN (FTE < 1 OR FTE IS NULL) AND [employment category] Like '*Full-Time*'
THEN 1
ELSE
CASE WHEN (FTE = 0 OR FTE IS NULL) AND [employment category] Like '*Part-Time*'
THEN 0.25
ELSE
CASE WHEN (SELECT COUNT(*) FROM SEC_EMP) = 0
THEN 1
ELSE
FTE
END
END
END
这是一些Fiddle示例。
如果您更喜欢 IIF 逻辑,请执行以下操作:
UPDATE EMP
SET fte_adj =
IIF ( (FTE < 1 OR FTE IS NULL) AND [employment category] Like '*Full-Time*' , 1 ,
IIF ( (FTE = 0 OR FTE IS NULL) AND [employment category] Like '*Part-Time*' , 0.25,
IIF ( (SELECT COUNT(*) FROM SEC_EMP) = 0 , 1 , FTE )
)
)
还有更多小提琴。
- 编辑 -
如果我了解最新评论,您想更新仅存在于 SEC_EMP 表中的记录吗?如果是这样,只需像这样加入表:
UPDATE E
SET fte_adj =
IIF ( (FTE < 1 OR FTE IS NULL) AND [employment category] Like '*Full-Time*' , 1 ,
IIF ( (FTE = 0 OR FTE IS NULL) AND [employment category] Like '*Part-Time*' , 0.25, FTE
)
)
FROM EMP E
JOIN SEC_EMP SE ON E.employee_id = SE.employee_id
还有更多小提琴。