Update multiple records in SQL
You can simply combine an update with a case statement such
UPDATE records
SET name =
CASE
WHEN id = 3 THEN 'abc'
WHEN id = 1 THEN 'def'
ELSE name
END
;WITH vals(id, name)
AS (SELECT 3,'abc'
UNION ALL
SELECT 1,'def')
UPDATE r
SET name = vals.name
FROM records r
JOIN vals
ON vals.id = r.id
For just a few records, you could use:
update records
set name = case id
when 1 then 'def'
when 3 then 'abc'
end
where id in (1, 3)
A bit more flexible is to create a result that you can join into the update:
update r
set name = x.name
from records r
inner join (
select id = 1, name = 'abc' union all
select 3, 'def' union all
select 4, 'qwe' union all
select 6, 'rty'
) x on x.id = r.id