I am using a subquery in an UPDATE:
UPDATE tableA
SET x,y,z = ( (SELECT x, y, z
FROM tableB b
WHERE tableA.id = b.id
AND (tableA.x != b.x
OR tableA.y != b.y
OR tableA.z != b.z))) );
My question is, what happens if the subquery returns no rows? Will it do an update with nulls?
Secondly, is there a better way to write this. I am basically updating three fields in tableA from tableB, but the update should only happen if any of the three fields are different.
On informix I used, a variation of Andomar's solution:
Yes-- you can test this like:
This will fill col1 with NULLs. In case the subquery returns multiple rows, like:
The database will generate an error.
Intuitively I wouldn't worry about the performance. If you really wish to avoid the update, you can write it like:
The
WHERE
clause prevents updates with NULL.