Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 JOIN vs NESTED SQL: Which is faster?

Author  Topic 

MMMY
Starting Member

14 Posts

Posted - 2012-08-15 : 19:03:09
Updating a table... Which would be faster? Or is there an even better option? Thanks!

QUERY 1
Update A
Set A.1 = B.1,
A.2 = B.2
From A
Join B on A.3 = B.3

QUERY 2
Update A
Set A.1 = (Select B.1 From B Where A.3 = B.3),
A.2 = (Select B.2 From B Where A.3 = B.3)

chadmat
The Chadinator

1974 Posts

Posted - 2012-08-15 : 19:06:06
I suspect Query 1 would be faster (Query 2 isn't a valid query, but I get what you mean)

-Chad
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-08-16 : 03:25:31
There is a logical difference between the two queries.
If there is no match between A table and B table; with query 2 the old values for colunms 1 and 2 remains the same as before.
With Query 2 (if there is no match) the values are replaced with a NULL values.

Query 2 also have the risk for failure if there are more than 1 match between table A and B.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -