Wednesday, 15 April 2015

sql - Update via subquery, what if the subquery returns no rows? -


I am using a subquery in an update:

  update table is a SET X, Y, Z = ((SELECT x, y, z to tableB b, where tableA.id = b.id and (tableA.x! = Bx or tableA.y! = OR OR tableA.z! = Bz))) ;  

My question is, what if the subquery does not give any rows? Will this make an update with the tap?

Second, this is a better way of writing. I am basically updating three areas from tableA to tableA, but only if there are three fields different.

What if the subquery does not give a line? Will it update with a tap?

Yes - you can test like this:

  set your tab col1 = (1 select where 1 = 0)  

This will fill the col1 with NULL if the subquery gives multiple rows, such as:

  set your tab set = 1 (select 1 union 2) < / Code> 

Database will generate an error

Second, it is a better way of writing. I am basically updating three fields from TableB to tableA, but only if there are three different fields.

I would not worry about performance if you really want to avoid updates, then you can write it like this:

  A SET x = bx, y = by, z = bz A from the table, table B, WHERE a.id = BID and (ax>   OR OR by & lt; & gt; BG)  

WHERE with section updates prevents NULL


No comments:

Post a Comment