简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM