如何在同时更新多个列的同时为多个列放置 where 子句?

发布于 2024-12-03 00:51:20 字数 1126 浏览 1 评论 0原文

我想同时更新 10 多个列,我的问题是我想为所有这些列添加 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 时才更新列。

例如,

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

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

是否可以放置 where 条件来检查每列?

任何建议表示赞赏..

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2024-12-10 00:51:20

像这样的东西(假设 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, '') 代替。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文