如果所有列均为负,则需要将其设置为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
是否可以在单个语句中执行相同的操作?
答案 0 :(得分: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)