Need quick help with a SQL query

brikis98

Diamond Member
Jul 5, 2005
7,253
8
0
It has been years since I've had to do anything more complicated with SQL than your basic SELECT query, so I can't quite recall the best way to do this. We are using PostgreSQL and Java.

I have a HashMap with a bunch of (id, value) pairs. Conveniently, I also have a table with the columns "id" and "value". I need a way to update all those columns in the table with the values from the HashMap. However, this will potentially involve changing millions of rows, so running a million separate UPDATE statements seems like a bad idea. Is there an efficient way to do a single, huge UPDATE statement that will set all the (id, value) pairs properly?




 

brandonbull

Diamond Member
May 3, 2005
6,365
1,223
126
if possible, make a backup of "table" then do the update

update table a
set value = (select b.value from HashMap b where a.id = b.id)