针对不同字段SQL Server的嵌套Case语句

时间:2016-07-17 07:53:30

标签: sql sql-server

我有一个案例陈述,它会检查几个条件,将数据输入名为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

你如何写第二个案例陈述?

3 个答案:

答案 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

总体思路是:

  1. 实际上完成了对 Column_1
  2. 的更新
  3. 转到 Column_2
    • 重复测试 Column_1 (或者更确切地说是他们各自的和相反的组合)(这部分可能需要一些扭曲以使逻辑正确。)
    • Column_2 添加额外测试。
  4. 请评论是否需要调整/进一步详细说明。

答案 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'