开发者

How to put where clause for multiple columns while updating multiple columns at the same time?

开发者 https://www.devze.com 2023-04-02 03:39 出处:网络
I want to update more than 10 columns at the same time , and my problem is I want to put where clause for all these columns.

I want to update more than 10 columns at the same time , and my problem is I want to put where clause for all these columns.

My code is:

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)

In the above statement I want to add condition as to update columns only when they are not 0.

for exmple,

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

    ..
     WHERE a.name <> 0,
            a.address <> 0, 
            a.telephone <> 0
            ....
         开发者_开发百科   ...

Is it possible to put where condition to check each column?

Any suggestions are appreciated..


Something like this (assuming name <> 0 is a typo and your names are really character columns)

 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

This essentially updates the column to it's current value if it's empty.

Note that this does not deal with NULL values! If you need to treat NULL and '' identically you need to use coalesce(name, '') instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消