同时更新多列

时间:2019-04-02 16:54:41

标签: sql teradata

我需要在同一查询中更新具有多个列的表(并且还要使用if else子句)可以指导我获得所需的输出。

update tablename 
set 
  IsEmailable=CASE 
    WHEN Email IS NOT NULL AND DNIInd <> 'Y' AND DoNotEmail <> 'Y' THEN 'Y' 
    ELSE 'N' 
  END,
  IsCallable=CASE 
    WHEN (ResTlphnNum IS NOT NULL OR CellTlphnNum IS NOT NULL) AND DoNotCallInd <> 'Y' AND DNIInd <> 'Y' THEN 'Y' 
    ELSE 'N' 
  END,
  IsMailable=CASE 
    WHEN AddrLine1Txt IS NOT NULL AND BadAddrInd <> 'Y' AND DoNotMailInd <> 'Y' AND DNIInd <> 'Y' THEN 'Y' 
    ELSE 'N' 
  END;

1 个答案:

答案 0 :(得分:1)

只需使用一个case语句:

update tablename set IsEmailable=CASE WHEN Email IS NOT NULL AND DNIInd='N' AND DoNotEmail='N' THEN 'Y' ELSE 'N' END, 

IsCallable=CASE WHEN (ResTlphnNum IS NOT NULL OR CellTlphnNum IS NOT NULL) AND DoNotCallInd='N' AND DNIInd='N' THEN 'Y' ELSE 'N' END,

IsMailable=CASE WHEN AddrLine1Txt IS NOT NULL AND BadAddrInd='N' AND DoNotMailInd='N' AND DNIInd='N' THEN 'Y' ELSE 'N' END