Skip to content
Advertisement

Sql update query using When condition

The example below apply null values into the field nom for the others id, how can forbid this action so that it does not modify the already existing values ​​and that I do not wish to modify ?

UPDATE dbo.a
SET [nom]= CASE
   WHEN [id]= 2  THEN 'uuo'
   else 
END

Advertisement

Answer

One way is with else:

UPDATE dbo.a
    SET [nom] = (CASE WHEN [id]= 2  THEN 'uuo' ELSE nom END);

But if you just want to update the rows that match, then use WHERE:

UPDATE dbo.a
    SET [nom] = 'uuo'
    WHERE [id] = 2;

No need to attempt to update every row if you know which one you want to change.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement