更新并选择一个语句(带有1个WHERE子句)

时间:2019-07-07 14:25:49

标签: sql sql-server sql-update where-clause

如何将以下SQL语句简化为1个WHERE子句:

SELECT * 
FROM tabA 
WHERE (colCar IS NULL OR colCar = '') 
   OR (colBike IS NULL OR colBike = '') 
   OR (colTrain IS NULL OR colTrain = '')

UPDATE tabA 
SET Distance = 999 
WHERE (colCar IS NULL OR colCar = '') OR 
      (colBike IS NULL OR colBike = '') OR 
      (colTrain IS NULL OR colTrain = '')

这句话没有道理,但工作正常。

我只想避免两次使用WHERE子句。

一些想法?

提前谢谢! 迈克

SQL Server

SELECT * 
FROM tabA 
WHERE (colCar IS NULL OR colCar = '') 
   OR (colBike IS NULL OR colBike = '') 
   OR (colTrain IS NULL OR colTrain = '')

UPDATE tabA 
SET Distance = 999 
WHERE (colCar IS NULL OR colCar = '') 
   OR (colBike IS NULL OR colBike = '') 
   OR (colTrain IS NULL OR colTrain = '')

代码运行正常

1 个答案:

答案 0 :(得分:0)

您可以使用OUTPUT子句返回更新的行:

UPDATE tabA 
SET Distance = 999
OUTPUT deleted.colCar
     , inserted.colCar
     , deleted.colBike
     , inserted.Distance
WHERE (colCar IS NULL OR colCar = '') OR 
      (colBike IS NULL OR colBike = '') OR 
      (colTrain IS NULL OR colTrain = '')