在CASE子句中添加三重条件

时间:2019-02-15 18:55:43

标签: sql sql-server tsql case

我有一个带有CASE子句的update子句,例如:

UPDATE [D]
                    SET
                       [DesignTypeGuid] = (CASE
                                         WHEN([D].[DesignNumber] < @CurrentContractedDesignNumber)
                                         THEN @BDTypeGuid
                                         ELSE @COTypeGuid
                                      END)

我的问题是我该如何添加第三个条件,我的意思是我也想做

[D].[DesignNumber] = @CurrentContractedDesignNumber THEN @CDTypeGuid

是否可以将其合并到现有案例中?

3 个答案:

答案 0 :(得分:4)

这是你的意思吗?

SET [DesignTypeGuid] = (CASE WHEN [D].[DesignNumber] < @CurrentContractedDesignNumber
                             THEN @BDTypeGuid
                             WHEN [D].[DesignNumber] = @CurrentContractedDesignNumber
                             THEN @CDTypeGuid
                             ELSE @COTypeGuid
                         END)

答案 1 :(得分:4)

只需在

时添加另一个
UPDATE [D]
SET  [DesignTypeGuid] = (CASE
                WHEN [D].[DesignNumber] < @CurrentContractedDesignNumber
                       THEN @BDTypeGuid
                WHEN [D].[DesignNumber] = @CurrentContractedDesignNumber T
                    HEN @CDTypeGuid
                                   ELSE @COTypeGuid
                                  END)

答案 2 :(得分:1)

WHEN . .表达式中添加其他CASE

(CASE WHEN [D].[DesignNumber] < @CurrentContractedDesignNumber
      THEN @BDTypeGuid
      WHEN [D].[DesignNumber] = @CurrentContractedDesignNumber
      THEN @CDTypeGuid                                   
      ELSE @COTypeGuid
 END)