2015-11-10 13 views

risposta

14

È possibile utilizzare tabella derivata:

update t set 
    value = a.value 
from Table as t 
    inner join (values 
     (22, 2), 
     (55, 5), 
     (99, 9) 
    ) as a(id, value) on a.id = t.id 

Per me questo è il modo più elegante per farlo ed è anche facilmente allungabile (puoi aggiungere più colonne se preferisci)

1

Sì, ma non come si deve:

UPDATE Table 
SET value=case when id=2 then 22 when id=5 then 55 when id=9 then 99 end 
WHERE id in (2,5,9) 

Se dovete fare questo per un gran numero di campi/record, si sarebbe meglio solo l'emissione di una serie di interrogazioni dedicato update .

0

utilizzare un'espressione CASE di scegliere valore di aggiornamento:

UPDATE Table SET value = case id when 2 then 22 
           when 5 then 55 
           when 9 then 99 
         end 
WHERE id IN (2,5,9) 
Problemi correlati