I want to update many rows in a single command with different IDs. These IDs should be passed in the query like
Update table
Set Updated = 1
where ID is in
(
1
2
3
)
The further difficulty is that there are two primary key columns to check for. I don’t want to update each row in its own command because its very slow because of the server roundtrips.
What is the best way to update a table like this?
Join the table to a derived table that contains a VALUES clause (SQL 2008) or a series of UNION ALL SELECTs.
For SQL 2005 and earlier:
Alternately, insert to a temp table and join to that. You could also create a stored procedure that accepts a parameter containing all the keys you need to join with. The parameter could be text (which you would split), xml, or a table-valued parameter.
If the amount of data is simply enormous then you might consider doing a bulk load of the keys from a text file into a table.