是否可以在一个语句中使用不同的条件更新多个列?

问题描述 投票:1回答:1

如果它们是负数,我需要将所有列设置为0。目前我一次只做一列:

UPDATE #ListingRank SET Distance = 0 WHERE Distance < 0
UPDATE #ListingRank SET ActionDays = 0 WHERE ActionDays < 0
UPDATE #ListingRank SET Price = 0 WHERE Price < 0
UPDATE #ListingRank SET Beds = 0 WHERE Beds < 0
UPDATE #ListingRank SET Baths = 0 WHERE Baths < 0
UPDATE #ListingRank SET SqFt = 0 WHERE SqFt < 0

是否可以在一个声明中做同样的事情?

sql sql-server sql-server-2017
1个回答
4
投票

你可以尝试使用CASE WHEN

UPDATE #ListingRank
SET Distance = (CASE WHEN Distance < 0 THEN 0 ELSE Distance END),
    ActionDays = (CASE WHEN ActionDays < 0 THEN 0 ELSE ActionDays END),
    Price = (CASE WHEN Price < 0 THEN 0 ELSE Price END),
    Beds = (CASE WHEN Beds < 0 THEN 0 ELSE Beds END),
    Baths = (CASE WHEN Baths < 0 THEN 0 ELSE Baths END),
    SqFt = (CASE WHEN SqFt < 0 THEN 0 ELSE SqFt END)
© www.soinside.com 2019 - 2024. All rights reserved.