2011-09-02 70 views
1

我想同時更新10列以上,我的問題是我想把所有這些列的where子句。如何在同時更新多個列的同時將多個列放在where子句中?

我的代碼是:

UPDATE Customer AS c 
        SET 
         name = a.name, 
         address= a.address, 
         telephone = a.telephone, 
         -- 
         -- 
         -- 


     FROM Customer a 
         INNER JOIN 
         (SELECT casenumber 
           , max(currentDate) AS md 
          FROM Customer 
          GROUP BY casenumber 
         ) AS z 
         ON z.casenumber = a.casenumber 
         AND z.md = a.currentDate 
        WHERE (a.casenumber = c.casenumber) 

在上述發言中,我要添加條件,以更新的列時,纔不爲0

爲exmple,

UPDATE Customer AS C 
    SET name = a.name, 
    address= a.address, 
    ... 

    .. 
    WHERE a.name <> 0, 
      a.address <> 0, 
      a.telephone <> 0 
      .... 
      ... 

是有可能把哪裏條件檢查每列?

任何建議都感激..

回答

2

像這樣的東西(假設name <> 0是一個錯字,你的名字是真的字符列)

UPDATE customer AS c 
    SET name = CASE WHEN name <> '' THEN a.name ELSE name END, 
     address = CASE WHEN address <> '' THEN a.address ELSE address END 

這基本上更新列到它的當前值,如果它是空的。

請注意,這不處理NULL值!如果您需要同樣處理NULL和'',則需要改爲使用coalesce(name, '')

+0

Thanyou這個解決了我的問題,但你想念CASE後WHEN .. –

+0

謝謝,更正 –