Update with selected values
The second form of UPDATE statement replaces the list
of assignments with a single bulk assignment, in which a list of columns
is set equal to a list of values. When the values are simple constants,
this form is nothing more than the form of the previous example with
its parts rearranged, as the following example shows:
UPDATE customer
SET (fname, lname) = ('Barnaby', 'Dorfler')
WHERE customer_num = 103;
No advantage exists to writing the statement this way. In fact, it is harder to read because it is not obvious which values are assigned to which columns.
However,
when the values to be assigned come from a single SELECT statement,
this form makes sense. Suppose that changes of address are to be applied
to several customers. Instead of updating the customer table
each time a change is reported, the new addresses are collected in
a single temporary table named newaddr. It contains columns
for the customer number and the address-related fields of the customer table.
Now the time comes to apply all the new addresses at once.
UPDATE customer
SET (address1, address2, city, state, zipcode) =
((SELECT address1, address2, city, state, zipcode
FROM newaddr
WHERE newaddr.customer_num=customer.customer_num))
WHERE customer_num IN (SELECT customer_num FROM newaddr);
A
single SELECT statement produces the values for multiple columns.
If you rewrite this example in the other form, with an assignment
for each updated column, you must write five SELECT statements, one
for each column to be updated. Not only is such a statement harder
to write, but it also takes much longer to execute.
Tip: In
SQL API programs, you can use record or host variables to update values.
For more information, refer to SQL programming.