0% found this document useful (0 votes)
29 views2 pages

Merge Statement Sql2008

The document discusses the MERGE statement in SQL Server 2008, which allows inserting, updating, and deleting records in a target table from a source table or query in a single atomic statement. Key points include that MERGE is set-based and more efficient than separate INSERT, UPDATE, DELETE statements. It also provides an example MERGE statement to update or insert customer records from a temporary table based on a join between the tables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Merge Statement Sql2008

The document discusses the MERGE statement in SQL Server 2008, which allows inserting, updating, and deleting records in a target table from a source table or query in a single atomic statement. Key points include that MERGE is set-based and more efficient than separate INSERT, UPDATE, DELETE statements. It also provides an example MERGE statement to update or insert customer records from a temporary table based on a join between the tables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

10/5/13

Merge Statement in SQL Server 2008 - CodeProject

Articles Database Database SQL Server

Merge Statement in SQL Server 2008


By Robin_Roy , 11 Jun 2009
4.36 (12 votes)

Introduction
One of the fantastic new features of SQL Server 2008 is Merge Statement. Using a single statement, we can Add/Update records in our database table, without explicitly checking for the existence of records to perform operations like Insert or Update.

Facts about Merge Statement


Here are a few facts that you must know before starting to use Merge Statement: 1. 2. 3. 4. Atomic statement combining I N S E R T ,U P D A T E and D E L E T E operations based on conditional logic Done as a set-based operation; more efficient than multiple separate operations M E R G E is defined by ANSI SQL; you will find it in other database platforms as well Useful in both OLTP and Data Warehouse environments OLTP: merging recent information from external source DW: incremental updates of fact, slowly changing dimensions.

A typical merge statement looks like:


M E R G E[ I N T O ]< t a r g e tt a b l e > U S I N G< s o u r c et a b l eo rt a b l ee x p r e s s i o n > O N< j o i n / m e r g ep r e d i c a t e >( s e m a n t i c ss i m i l a rt oo u t e rj o i n ) W H E NM A T C H E D< s t a t e m e n tt or u nw h e nm a t c hf o u n di nt a r g e t > W H E N[ T A R G E T ]N O TM A T C H E D< s t a t e m e n tt or u nw h e nn om a t c hf o u n di nt a r g e t >

Example
-U p d a t ee x i s t i n g ,a d dm i s s i n g M E R G EI N T Od b o . t b l _ C u s t o m e r sA SC
www.codeproject.com/Articles/37172/Merge-Statement-in-SQL-Server-2008?display=Print 1/3

10/5/13

Merge Statement in SQL Server 2008 - CodeProject

U S I N Gd b o . t b l _ C u s t o m e r s T e m pA SC T O NC . C u s t I D=C T . C u s t I D W H E NM A T C H E DT H E N U P D A T ES E T C . C o m p a n y N a m e=C T . C o m p a n y N a m e , C . P h o n e=C T . P h o n e W H E NN O TM A T C H E DT H E N I N S E R T( C u s t I D ,C o m p a n y N a m e ,P h o n e ) V A L U E S( C T . C u s t I D ,C T . C o m p a n y N a m e ,C T . P h o n e ) C R E A T ET A B L Ed b o . t b l _ S o u r c e( i dI N T ,n a m eN V A R C H A R ( 1 0 0 ) ,q t yI N T ) ; C R E A T ET A B L Ed b o . t b l _ T a r g e t( i dI N T ,n a m eN V A R C H A R ( 1 0 0 ) ,q t yI N T ) ; S y n c h r o n i z es o u r c ed a t aw i t ht a r g e t M E R G EI N T Od b o . t b l _ T a r g e tA St U S I N Gd b o . t b l _ S o u r c eA Ss O Nt . i d=s . i d W H E NM A T C H E DA N D( t . n a m e! =s . n a m eO Rt . q t y ! =s . q t y )T H E N R o we x i s t sa n dd a t ai sd i f f e r e n t U P D A T ES E Tt . n a m e=s . n a m e ,t . q t y=s . q t y W H E NN O TM A T C H E DT H E N R o we x i s t si ns o u r c eb u tn o ti nt a r g e t I N S E R TV A L U E S( s . i d ,s . n a m e ,s . q t y ) W H E NS O U R C EN O TM A T C H E DT H E N R o we x i s t si nt a r g e tb u tn o ti ns o u r c e D E L E T EO U T P U T $ a c t i o n ,i n s e r t e d . i d ,d e l e t e d . i d

Conclusion
So now with this new feature, we can implement the feature of add/insert/delete using a single statement without checking through the records. Hope you enjoyed this article. Happy programming!!!

History
11 th June, 2009: Initial post

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

www.codeproject.com/Articles/37172/Merge-Statement-in-SQL-Server-2008?display=Print

2/3

You might also like