SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Confio Ignite

Basic MERGE statements in SQL Server 2008

The MERGE statement is a new feature in SQL Server 2008 that implements upsert capabilities (insert if the row is not present, update if the row is present). This video goes over the basics of the MERGE statement in SQL Server 2008.

Duration:
3 mins 50 secs
Skill Level:
100
Rating:
4.49 out of 5
Publish Date:
November 03, 2008
Basic MERGE statements in SQL Server 2008 You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Plamen Ratchev
Plamen Ratchev has over two decades experience in the software architecture and development field. He has enjoyed building solutions for UNIX, DOS and Windows platforms, with main focus on SQL Server since version 4.21. Plamen is founder of Tangra, specializing in relational database applications analysis, implementation, and tuning. His particular interest is in design patterns, performance a...

References

There are no downloads or recommended reading links for this video

Comments
Dharmendrakumar on 1/1/2009
Perfect video!!!

M Hoss on 2/19/2009
your answer shows that... at the when matched or not matched you can insert, update or Delete. I thought the whole idea of this was to do an UpSert, no Delete in statement. Please let me know if this is right or wrong.

Leonard Peoples on 2/19/2009
It's perfect.

Tariq Bilal on 2/19/2009
Good Video. Thanks

Michelle Poolet on 2/19/2009
Hard to understand speaker, but great new content and a good argument for upgrading to 2008!

Carla Wilson on 2/19/2009
Cool! Can't wait to use it! Nice example. Thanks.

Anthony Zackin on 2/19/2009
No, it is good but not perfect. It does not speak to the issue of unnecessary updates being performed due to matching on just the primary key. Should not the ON clause include all of the fields in the update statement to preclude this?

D. K. Kiteme on 2/19/2009
Very informative but bad sound quality.

Gautam on 2/19/2009
Pretty Good and Concise explanation of Merge

Plamen Ratchev on 2/20/2009
M Hoss, the idea here is to demonstrate the MERGE statement, which supports full synchronization between two tables (including a DELETE). However, if you need to perform only an UPSERT, then you can simply skip the WHEN NOT MATCHED BY SOURCE clause and this will have the effect of UPSERT.

Plamen Ratchev on 2/20/2009
Anthony, this video as it is labeled is only to demonstrate the basic syntax of the MERGE statement. To see the full syntax of the MERGE statement, including using additional predicates to avoid unnecessary updates, please see the video "The MERGE statement in SQL Server 2008", http://www.jumpstarttv.com/the-merge-statement-in-sql-server-2008_520.aspx.The correct way to handle unnecessary updates is to use additional predicates in the WHEN MATCHED clause, not to add more join conditions in the ON clause.

ML Stephens on 2/20/2009
thanks, you've shown how simple and amazing it is

Tim Mitchell on 2/20/2009
Good explanation of this new SQL Server 2008 functionality.

Larry on 2/23/2009
Fun!

Philip Hickey on 2/26/2009
Excellent video.

Carl Draves on 3/2/2009
What I was looking for. Great...

Rhonda on 3/21/2009
Very good content, well delivered

Edward Thompson on 4/11/2009
Nice clean explanation. Thank you.

datamama on 5/15/2009
I really appreciate like the slow and careful explanation of the example.

joro on 7/29/2009
super e :)

Mickey on 9/22/2009
THANK YOU! Very well documented and easy to follow

James Lawrence on 2/26/2010
Very useful statement and good explanation and example!

mark brito on 5/29/2010
Awesome feature. Never herad of it before.

srinivas on 10/25/2010
Good one.

Charlie Bruno on 10/31/2010
Nice presentation.

Dennis Allen on 11/9/2010
I would like to see more complex examples of MERGE syntax as well as performance comparisons between using MERGE verses independent DELETE (where missing source or source != target) and then INSERT (where target missing source).

Plamen Ratchev on 11/10/2010
Dennis, here are two examples of MERGE that demonstrate more advanced techniques: http://www.sqlshare.com/the-merge-statement-in-sql-server-2008_520.aspx; http://www.sqlshare.com/composable-dml-in-sql-server-2008_515.aspx

nilesh bilimoria on 11/17/2010
great job with this.

MarkAllen on 3/16/2011
awesome, I can think of so many uses for it already

Sur Man on 5/27/2011
Thank you. Nicely explained.

Daniel Frankel on 6/16/2011
What I was unsure of in your demo was what if your update statement requires more than one table to join on

Plamen Ratchev on 6/21/2011
Daniel, you meed a source and a target table. It can be the same table serving as source and target. There is no requirement to join with more tables, but you can if needed. Think of the USING statement as FROM clause. You can use a single table or a join.

eric camplin on 6/27/2011
should publish the code and/or run against a standard sample database

Laxman on 7/4/2011
Very useful video.

David Mark Everson on 7/20/2011
how does the Merge statement handle source table deleted rows?

Mark Horninger on 7/20/2011
Author is hard to understand, he has heavy foriegn accent.

Tim Eichelberger on 7/20/2011
Quick and simple explanation of the MERGE statement. Great job!

Leigh Anne on 7/20/2011
Very helpful!

Farid on 7/20/2011
it endend too early

David Doyle on 7/20/2011
Can you update only when there are changes to the values for the existing columns? Otherwise you will be updating potentially a lot of rows that really have not changed.

Dave Bennett on 7/20/2011
Good content. Audio could have been clearer. Could have shown the before of the two tables with actual queries rather than just the raw sql.

Shellz on 7/20/2011
Great introduction to this topic!!

Ameena on 7/20/2011
Very nicely done video.

Zeshan on 7/20/2011
Really fine.

sudharshan on 7/20/2011
gud video,helps a lot

Shashank Banerjea on 7/21/2011
It would have been helpful to see the contents of the target table - CentralOfficeAccounts before the merge statement was executed. Overall a great tutorial.

MohanD on 7/21/2011
Good one...

8FF3F64A4D on 7/21/2011
great info in a short video

Peter Voutov on 7/21/2011
Would be interesting to mention how Merge handles identity columns

Susan McLoughlin on 7/21/2011
It was hard understanding what he was saying.

John O'Sullivan on 7/22/2011
excellent video

Derek on 7/22/2011
Pretty straightforward and easy to understand.

Sunil on 7/23/2011
Excellent

tshel on 7/24/2011
Great Video Thanks! I can see using it possibly in SSIS packages rather than the more complicated slowly changing dimension task. Adding a checksum computed column to the source table and to the target tables too to compare the fields to preclude any unnecessary updates as well could be handy saving on processing/performance.

Plamen Ratchev on 7/24/2011
Shashank Banerjea, you can see the scripts to populate the tables in my article here: http://pratchev.blogspot.com/2008/03/upsert-and-more-with-merge.html

Plamen Ratchev on 7/24/2011
Peter Voutov, there is nothing special about IDENTITY columns. They are handled the same way as in regular INSERT/UPDATE statements. That is you cannot assign/update value for these columns, they are handled automatically on INSERT action.

Plamen Ratchev on 7/24/2011
David Mark Everson, see the last query in my article here: http://pratchev.blogspot.com/2008/03/upsert-and-more-with-merge.html. It shows how you can use the clause WHEN SOURCE NOT MATCHED THEN DELETE to delete the rows in the target table.

Plamen Ratchev on 7/24/2011
David Doyle, please see the second MERGE query in my article: http://pratchev.blogspot.com/2008/03/upsert-and-more-with-merge.html. It shows how you can use predicates in the WHEN clause to update only rows that have not changed.

Eric on 7/25/2011
Thank you for a very clear example of the MERGE statement.

David McDonald on 7/29/2011
usually inserting and updating occurs when reading in data from another source, like a CSV file perhaps. This demo shows that you have to create a target table with the same Keys as the source table. How is one to know this? Also there seems to then be a need to delete the source table. Explanations were missing on how the tables need to be first setup and then what to do with one after the MERGE was done. I had a lot of questions after viewing the video.

Charels Graysmark on 7/29/2011
what about delete when records are in the target but not in the source

Plamen Ratchev on 7/31/2011
Charels, you can use the clause WHEN SOURCE NOT MATCHED THEN DELETE to delete the rows in the target table. You cannot delete rows from the source table, all operations are performed on the target table only.

ram on 8/4/2011
Can we have one more condition in addition to "When Matched"?

Plamen Ratchev on 8/5/2011
Ram, the MERGE statement can have at most two WHEN MATCHED clauses. If two clauses are specified, then the first clause must be accompanied by an "AND search_condition" clause. For any given row, the second WHEN MATCHED clause is only applied if the first is not. If there are two WHEN MATCHED clauses, then one must specify an UPDATE action and one must specify a DELETE action.

Shane Gleeson on 8/12/2011
Very clear explanation

Jose on 8/12/2011
A very demonstration and explanation!

Libby on 8/12/2011
Good to know of this new option!

Elmozamil on 8/13/2011
it is a good vedio about the Merge statement, and the best thing that the simplist explanation of it that you use. Thanks Alot

Robert on 8/29/2011
So good that I enjoyed so much.

Nick on 10/7/2011
would have been nice to see how to do deletes.

Plamen Ratchev on 10/8/2011
Nick, the following video demonstrates how to use the DELETE statement with MERGE: http://www.sqlshare.com/the-merge-statement-in-sql-server-2008_520.aspx

Gary Mazzone on 10/17/2011
I think it is a little misleading in the Update portion of the video. The speaker explains that one record will be updated (the one which adds the , INC to the name). But the unchanged record is updated also since it falls into then WHEN MATCHED clause.

blaze on 10/18/2011
WHAT ABOUT DELETED ROWS?

Plamen Ratchev on 10/18/2011
Gary, the following video demonstrates how to add predicates in the WHEN clause to avoid updating rows that have not changed: http://www.sqlshare.com/the-merge-statement-in-sql-server-2008_520.aspx

Plamen Ratchev on 10/18/2011
Blaze, the following video demonstrates how to handle deleted rows: http://www.sqlshare.com/the-merge-statement-in-sql-server-2008_520.aspx

Rolan Logan on 12/9/2011
Good info. Thank you!

A4F522F34E on 12/18/2011
The MERGE statement is a new great feature which I'll use tomorow ...

A Alagu Ganesh on 3/30/2012
Good

Doug on 9/11/2012
That was AWESOME! Thanks Guys!



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions