我有一个案例陈述,它会检查几个条件,将数据输入名为Column_1
的特定字段。
由于你不能修改同一个case语句中的两个字段,我觉得最好的选择是嵌套另一个case语句来修改第二个字段。
我的代码如下。
UPDATE Staff_Manager.dbo.Staff_Time_TBL
SET Column_1 = CASE
WHEN Column_1 != NULL AND Time_Data_1 IS NOT NULL AND Time_Data_2 IS NOT NULL THEN 'Day'
WHEN Time_Data_1 IS NULL AND Time_Data_2 IS NULL THEN 'Hol'
ELSE
SET Column_2 = CASE
WHEN Time_Data_3 IS NULL THEN 'Something'
ELSE NULL
END
END
WHERE Staff_No = 1903 AND Date_Data BETWEEN '2016/2/1' AND '2016/3/1'
因此,在上面的代码中,当column_1
的条件抛出ELSE
时,我希望针对新条件测试不同的列Column_2
。
你如何写第二个案例陈述?
答案 0 :(得分:2)
你的案子有错误,试试这个:
UPDATE Staff_Manager.dbo.Staff_Time_TBL
SET Column_1 = CASE
WHEN Column_1 is not NULL AND Time_Data_1 IS NOT NULL AND Time_Data_2 IS NOT NULL THEN 'Day'
WHEN Time_Data_1 IS NULL AND Time_Data_2 IS NULL THEN 'Hol'
else Column_1 end,
Column_2= CASE
WHEN Column_1 is not NULL AND Time_Data_1 IS NOT NULL AND Time_Data_2 IS NOT NULL THEN Column_2
WHEN Time_Data_1 IS NULL AND Time_Data_2 IS NULL THEN Column_2
WHEN Time_Data_3 IS NULL THEN 'Something'
ELSE NULL
END
WHERE Staff_No = 1903 AND Date_Data BETWEEN '2016/2/1' AND '2016/3/1'
答案 1 :(得分:1)
这应该让你开始:
UPDATE Staff_Manager.dbo.Staff_Time_TBL
SET Column_1 = CASE
WHEN Column_1 IS NOT NULL AND Time_Data_1 IS NOT NULL AND Time_Data_2 IS NOT NULL THEN 'Day'
WHEN Time_Data_1 IS NULL AND Time_Data_2 IS NULL THEN 'Hol'
ELSE Column_1
END,
Column_2 = CASE
WHEN NOT (Column_1 IS NOT NULL AND Time_Data_1 IS NOT NULL AND Time_Data_2 IS NOT NULL
AND
Time_Data_1 IS NULL AND Time_Data_2 IS NULL)
AND
Time_Data_3 IS NULL
THEN 'Something'
END
总体思路是:
请评论是否需要调整/进一步详细说明。
答案 2 :(得分:0)
试试这个,
UPDATE Staff_Manager.dbo.Staff_Time_TBL
SET Column_1 = (CASE
WHEN Column_1 is not NULL AND Time_Data_1 IS NOT NULL AND Time_Data_2 IS NOT NULL THEN 'Day'
WHEN Time_Data_1 IS NULL AND Time_Data_2 IS NULL THEN 'Hol'
ELSE Column_1 end)
,Column_2 = case when (CASE
WHEN Column_1 is not NULL AND Time_Data_1 IS NOT NULL AND Time_Data_2 IS NOT NULL THEN 'Day'
WHEN Time_Data_1 IS NULL AND Time_Data_2 IS NULL THEN 'Hol' else 'Error' end)='Error' and
Time_Data_3 IS NULL THEN 'Something' ELSE Column_2 --(Here you have to handle else condition as per requirement)
END
WHERE Staff_No = 1903 AND Date_Data BETWEEN '2016/2/1' AND '2016/3/1'