SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Best way to UPDATE with join to other tables...


Best way to UPDATE with join to other tables...

Author
Message
PiMané
PiMané
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1046 Visits: 1334
Hi,

What's the best way, so locks can be avoided and other problems, a table with joins to other ones?
I read that using alias on the table being updated can cause locks and I see o lot of those...

UPDATE t SET t.c0 = j.c0, t.c1 = j.c1 FROM tableToUpdate t INNER JOIN joinTable j ON t.Id = p.IdFromT

-- vs --

UPDATE tableToUpdate SET tableToUpdate.c0 = j.c0, tableToUpdate.c1 = j.c1 FROM joinTable j WHERE tableToUpdate .Id = p.IdFromT



Thanks,
Pedro



If you need to work better, try working less...
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3107 Visits: 2766
First one is better...
I use first option generally

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16770 Visits: 19121
Locks will happen on both updates. The difference might be the JOIN type which uses different standards. The first query uses ANSI-92 standard and the second one uses ANSI-89 standard. AFAIK, both queries will be interpreted the same way.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27683 Visits: 13268
Personally I use the first options as it is much clearer on what you want to achieve.
I think Luis is correct in asserting that both queries are interpreted the same way. You could check the query plan to make sure.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5344 Visits: 4076
oops... i havent found any diference except alias used in first query ? w00t , did i overlook something ?

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Keith Tate
Keith Tate
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1032 Visits: 979
If you create those tables and add some test data and run both queries you will notice that you get the same exact plan. The two statements are identical.

As far as the locks go they will obtain the same locks you can't avoid locks being take on an update operation. It will have to request update locks are all rows that will be updated and then those locks will move to exclusive locks once they are all ready.

So it come down to readability and for that I prefer the first query using the "join" statement.



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
Kurt W. Zimmerman
Kurt W. Zimmerman
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1410 Visits: 1398
Luis Cazares (10/21/2013)
Locks will happen on both updates. The difference might be the JOIN type which uses different standards. The first query uses ANSI-92 standard and the second one uses ANSI-89 standard. AFAIK, both queries will be interpreted the same way.


OK, so looking at both queries I'd have to say I'm "old school" cuz I would have written it the second way. Whistling

Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY

http://www.linkedin.com/in/kurtwzimmerman
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14376 Visits: 15980
Beware, though - if you have multiple matches in joinTable for any rows in tableToUpdate then you'll get unexpected results. This is known as a cardinality error, and the frightening thing is that you won't know it's happened, because there'll be no error or warning message.

John
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16770 Visits: 19121
Kurt W. Zimmerman (10/21/2013)
Luis Cazares (10/21/2013)
Locks will happen on both updates. The difference might be the JOIN type which uses different standards. The first query uses ANSI-92 standard and the second one uses ANSI-89 standard. AFAIK, both queries will be interpreted the same way.


OK, so looking at both queries I'd have to say I'm "old school" cuz I would have written it the second way. Whistling

I would suggest you to change your habits as the outer joins in the ANSI-89 standard are no longer supported on SQL Server. Who knows, maybe they'll force complete ANSI-92 standard in future versions.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Kurt W. Zimmerman
Kurt W. Zimmerman
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1410 Visits: 1398
Luis Cazares (10/21/2013)
Kurt W. Zimmerman (10/21/2013)
Luis Cazares (10/21/2013)
Locks will happen on both updates. The difference might be the JOIN type which uses different standards. The first query uses ANSI-92 standard and the second one uses ANSI-89 standard. AFAIK, both queries will be interpreted the same way.


OK, so looking at both queries I'd have to say I'm "old school" cuz I would have written it the second way. Whistling

I would suggest you to change your habits as the outer joins in the ANSI-89 standard are no longer supported on SQL Server. Who knows, maybe they'll force complete ANSI-92 standard in future versions.


Some habits are easy to break... This is one of them.

Thanks Luis.

Kurt

Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY

http://www.linkedin.com/in/kurtwzimmerman
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search