views:

254

answers:

2

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.

+1  A: 

what happens if the subquery returns no rows? Will it do an update with nulls?

Yes-- you can test this like:

update YourTable
set col1 = (select 1 where 1=0)

This will fill col1 with NULLs. In case the subquery returns multiple rows, like:

update YourTable
set col1 = (select 1 union select 2)

The database will generate an error.

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.

Intuitively I wouldn't worry about the performance. If you really wish to avoid the update, you can write it like:

UPDATE a
SET x = b.x, y = b.y, z = b.z
FROM tableA a, tableB b 
WHERE a.id = b.id AND (a.x <> b.x OR a.y <> b.y OR a.z <> b.z)

The WHERE clause prevents updates with NULL.

Andomar
That looks brilliant, except, I forgot to mention I am using informix IDS 11.5, and I don't believe informix supports FROM in the UPDATE or joins in UPDATEs.
prmatta
@prmatta: Informix does support the FROM in UPDATE I think; I've removed the INNER JOIN.
Andomar
@Andomar, I don't think it does, atleast not mine. The documentation says it does, but I kept getting a syntax error. I have updated the sql to something without froms and joins and put it in as an answer, let me know if it looks good to you.
prmatta
@prmatta: Your solution looks like a good work around for the Informix limitation!
Andomar
A: 

On informix I used, a variation of Andomar's solution:

UPDATE a
SET x,y,z = ( (SELECT x, y, z 
               FROM tableB b
               WHERE tableA.id = b.id) )
WHERE tableA.id IN (SELECT fromTable.id
                    FROM tableA toTable, tableB fromTable
                    WHERE toTable.id = fromTable.id
                      AND ((toTable.x <> fromTable.x) 
                           OR (toTable.y <> fromTable.y)
                           OR (toTable.z <> fromTable.z))
prmatta