UPDATE …. FROM JOIN —(Why Merge…) T-SQL


You can find a quick sample case to see that sometimes when JOIN generates duplicates rows, your UPDATE will create wrong results without notice. But you can use Merge to avoid this loophole. You will catch the error:

The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than one source row. A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times. Refine the ON clause to ensure a target row matches at most one source row, or use the GROUP BY clause to group the source rows.

http://www.sqlservercentral.com/articles/T-SQL/101464/

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s