Combining two UPDATE statement with one SET statement with WHERE clause and one SET without

StackOverflow https://stackoverflow.com/questions/23672068

  •  23-07-2023
  •  | 
  •  

Frage

Is it possible to combine this query into one UPDATE statement?

UPDATE [dbo].[MyTable]
SET Column1 = Column2

UPDATE [dbo].[MyTable]
SET Column3 = 1
WHERE Column1 IS NOT NULL

Thanks.

War es hilfreich?

Lösung

UPDATE [DBO].[MyTable]
SET Column1 = Column2,
    Column3 = (
  CASE
    WHEN Column1 IS NOT NULL
    THEN 1
    ELSE Column3
  END)

Andere Tipps

This should work for you:

UPDATE [dbo].[MyTable]
SET Column1 = Column2
    ,Column3 = 
    case 
         when Column1 IS NOT NULL THEN 1
         else Column3 
     end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top