Speeding UPDATEs Using the CASE Statement
We can use the CASE statement to update multiple columns in a table, even using separate update criteria for each column. This example updates the publishers table to set the state column to “–” for non-USA companies, and changes the city for one particular publisher, all in one table read operation.
SET state =
WHEN country <> “USA”
WHEN pub_id = “9999″
WHERE country <> “USA” OR
pub_id = “9999″
The same format will work for updates across three or more rows with different update criteria.
You may come across fewer opportunities to use this second technique efficiently. This query will almost invariably result in a table scan because we are selecting on multiple columns that are unlikely to all be in a covering index. If each column is updated only a small number of times, and is indexed, it may still be more efficient to do separate updates.
A good place to use this technique might be in cleaning up multiple columns in a long interface file from another system.
Because we are using two separate CASE statements, one for each test criteria/update, each CASE statement will be evaluated for every row, and updates applied where required. Therefore, if more than one column in the row requires an update, they will all be updated.
Two things are particularly important to remember in this example:
The ELSE [column] clause is required for each case statement used, otherwise you will end up nulling-out data you do not want to.
The WHERE clause at the end must be used to restrict the update to rows that require at least one column updating, otherwise every column in the table will be updated, increasing both execution time and pressure on the transaction log.
Pages: 1 2