<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Edwin M Sarmiento&gt;MERGE statement in SQL Server 2008 &#8211; Edwin M Sarmiento</title>
	<atom:link href="https://www.edwinmsarmiento.com/merge-statement-in-sql-server-2008/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.edwinmsarmiento.com</link>
	<description>Intentional Excellence</description>
	<lastBuildDate>Mon, 13 Apr 2026 21:00:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">84283043</site>		<item>
		<title>&gt;MERGE statement in SQL Server 2008</title>
		<link>https://www.edwinmsarmiento.com/merge-statement-in-sql-server-2008/</link>
		<comments>https://www.edwinmsarmiento.com/merge-statement-in-sql-server-2008/#respond</comments>
		<pubDate>Wed, 10 Oct 2007 09:59:00 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/2007/10/10/merge-statement-in-sql-server-2008</guid>

				<description><![CDATA[&#62;The MERGE statement in SQL Server 2008 is a new DML statement that combines multiple DML operations. It performs INSERT, UPDATE, or DELETE operations on a target table based on the results of a join with a source table. This statement enables you to merge two tables based on a set of criteria. In previous [&#8230;]]]></description>
					<content:encoded><![CDATA[<p>&gt;<span style="font-family:arial;">The <strong><span style="font-size:85%;">MERGE</span></strong> statement in <strong><span style="font-size:85%;">SQL Server 2008</span></strong> is a new DML statement that combines multiple DML operations. It performs INSERT, UPDATE, or DELETE operations on a target table based on the results of a join with a source table. This statement enables you to merge two tables based on a set of criteria. In previous versions of SQL Server, you have to create separate statements if you need to insert, update, or delete data in one table based on certain conditions in another table or query. With <strong><span style="font-size:85%;">MERGE</span></strong>, you can include the logic for these data modifications in one statement. You specify a source record set, which could be a table join or a view, and a target table, and the join condition between the two. You then specify the DML statement that you want to execute when the records between the two data are matched or are not matched. One typical use for this is bulk loading data warehouse tables or maybe executing DML statements during off-peak hours. To demonstrate how the <strong><span style="font-size:85%;">MERGE</span></strong> statement works, here&#8217;s a very simple example. I&#8217;ll create two tables and populate them with values.</span></p>
<p><span style="font-family:lucida grande;font-size:85%;"><strong><span style="color:#000099;">USE</span> Northwind</strong></span><br /><span style="font-family:lucida grande;font-size:85%;"><strong>GO<br /><span style="color:#000099;">CREATE TABLE</span> Table1 (ID <span style="color:#000099;">int</span>, [Value] <span style="color:#000099;">varchar</span>(20), Comment <span style="color:#000099;">varchar</span>(50))<br /><span style="color:#000099;">CREATE TABLE</span> Table2 (ID <span style="color:#000099;">int</span>, [Value] <span style="color:#000099;">varchar</span>(20), Comment <span style="color:#000099;">varchar</span>(50))<br />GO<br /><span style="color:#000099;">INSERT INTO</span> Table1 <span style="color:#000099;">VALUES</span> (1, <span style="color:#990000;">&#8216;A&#8217;</span>, <span style="color:#990000;">&#8216;Inserted&#8217;</span>), (2, <span style="color:#990000;">&#8216;B&#8217;</span>, <span style="color:#990000;">&#8216;Inserted&#8217;</span>)<br /><span style="color:#000099;">INSERT INTO</span> Table2 <span style="color:#000099;">VALUES</span> (1, <span style="color:#990000;">&#8216;C&#8217;</span>, <span style="color:#660000;">&#8216;Inserted&#8217;</span>), (3, <span style="color:#990000;">&#8216;D&#8217;</span>, <span style="color:#990000;">&#8216;Inserted&#8217;</span>)<br />GO</strong></span></p>
<p><span style="font-family:arial;">Here is where I&#8217;ll be using the <strong><span style="font-size:85%;">MERGE</span></strong> statement. What I&#8217;ll do is I&#8217;ll use <strong><span style="font-size:85%;">Table1</span></strong> as my source data and <strong><span style="font-size:85%;">Table2</span></strong> as my target table. I&#8217;ll look at <strong><span style="font-size:85%;">Table1</span></strong> based on the <strong><span style="font-size:85%;">ID</span></strong> column and, if a match is found, I&#8217;ll update the <strong><span style="font-size:85%;">Comment</span></strong> column with a value <strong><span style="font-size:85%;">Match Found from Source</span></strong>. If the row from the <strong><span style="font-size:85%;">Table1</span></strong> does not match another row in <strong><span style="font-size:85%;">Table2</span></strong>, I&#8217;ll insert a new row and specify the <strong><span style="font-size:85%;">Comment</span></strong> column with a value <strong><span style="font-size:85%;">No Match Found on Target</span></strong>. If the row from <strong><span style="font-size:85%;">Table2</span></strong> does not match another row from <strong><span style="font-size:85%;">Table1</span></strong>, I&#8217;ll update the <strong><span style="font-size:85%;">Comment</span></strong> column value to <strong><span style="font-size:85%;">No Match Found from Source</span></strong>. The MERGE statement for this scenario looks like this</span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:lucida grande;font-size:85%;"><strong><span style="color:#000099;">MERGE </span>Table2 <span style="color:#000099;">AS</span> targetTable<br />USING (<span style="color:#000099;">SELECT</span> ID, [Value] <span style="color:#000099;">FROM</span> Table1) SourceTable<br /><span style="color:#000099;">ON</span> (targetTable.ID = SourceTable.ID)<br /><span style="color:#000099;">WHEN</span> MATCHED <span style="color:#000099;">THEN UPDATE SET</span> Comment = <span style="color:#990000;">&#8216;Match Found from Source&#8217;</span><br /><span style="color:#000099;">WHEN</span> TARGET <span style="color:#666666;">NOT</span> MATCHED <span style="color:#000099;">THEN INSERT VALUES</span> (ID, [Value], <span style="color:#990000;">&#8216;No Match Found on Target&#8217;</span>)<br /><span style="color:#000099;">WHEN</span> SOURCE <span style="color:#666666;">NOT</span> MATCHED <span style="color:#000099;">THEN UPDATE SET</span> Comment = <span style="color:#990000;">&#8216;No Match Found from Source&#8217;</span>;<br />GO</strong></span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:Arial;">If you analyze the query, it will update the first record in <strong><span style="font-size:85%;">Table2</span></strong> with <strong><span style="font-size:85%;">ID=1</span></strong> and set the <strong><span style="font-size:85%;">Comment</span></strong> field value to <span style="font-size:85%;"><strong>Match Found from Source </strong></span><span style="font-size:100%;">as they both have the same values</span>. Since the second record in <strong><span style="font-size:85%;">Table1</span></strong> has a an <strong><span style="font-size:85%;">ID</span></strong> value of <strong><span style="font-size:85%;">2</span></strong>, it will update the <strong><span style="font-size:85%;">Comment</span></strong> field value to <strong><span style="font-size:85%;">No Match Found on Target</span></strong> on <strong><span style="font-size:85%;">Table2</span></strong> since the <strong><span style="font-size:85%;">ID</span></strong> column has a value of <span style="font-size:85%;"><strong>3. </strong></span><span style="font-size:100%;">And since the <strong><span style="font-size:85%;">Table2</span></strong> has a record with no matching <strong><span style="font-size:85%;">ID</span></strong> value from </span><span style="font-size:85%;"><strong>Table1</strong>, </span><span style="font-size:100%;">it will update <strong><span style="font-size:85%;">Comment</span></strong> field value to <strong><span style="font-size:85%;">No Match Found on Target. </span></strong></span></span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:Arial;">Having explained how the concept of the <strong><span style="font-size:85%;">MERGE</span></strong> statement works, let&#8217;s look at a practical application. Using the <strong><span style="font-size:85%;">Northwind</span></strong> database, I have created a script which will update the <strong><span style="font-size:85%;">Products</span></strong> table based on the sales made for the day. It will subtract the total number of units for a specific product from the <strong><span style="font-size:85%;">Order Details</span></strong> table from the UnitsInStock column of the <strong><span style="font-size:85%;">Products</span></strong> table. You can run this query at the end of the day when there is not much queries running during the day. Normally, we would either create a trigger or a stored procedure which is wrapped in a transaction to solve this problem. </span></p>
<p><strong><span style="font-size:85%;"><span style="color:#000099;">USE</span> Northwind</span></strong><br /><strong><br /><span style="font-size:85%;"><span style="color:#000099;">MERGE</span> Products <span style="color:#000099;">AS</span> P<br />USING (<span style="color:#000099;">SELECT </span>ProductID, <span style="color:#cc33cc;">SUM</span>(Quantity) <span style="color:#000099;">FROM</span> [Order Details] OD<br /><span style="color:#666666;">JOIN</span> Orders O<br /><span style="color:#000099;">ON</span> OD.OrderID = O.OrderID<br /><span style="color:#666666;">AND</span> O.OrderDate = <span style="color:#cc33cc;">GETDATE</span>()<br /><span style="color:#000099;">GROUP BY</span> ProductID) <span style="color:#000099;">AS</span> src (ProductID, OrderQty)<br /><span style="color:#000099;">ON</span> (P.ProductID = src.ProductID)<br /><span style="color:#000099;">WHEN</span> MATCHED <span style="color:#666666;">AND</span> P.UnitsInStock &#8211; src.OrderQty  0<br /><span style="color:#000099;">THEN UPDATE SET</span> P.UnitsInStock = P.UnitsInStock &#8211; src.OrderQty<br /><span style="color:#000099;">WHEN</span> MATCHED <span style="color:#666666;">AND</span> P.UnitsInStock &#8211; src.OrderQty = 0<br /><span>THEN DELETE</span>;</span></strong><br /><span style="font-family:Arial;"></span><br /><span style="font-family:arial;">Check out my SQL Server 2008 videos at </span><a href="http://blogcastrepository.com/level5/sql2008/"><span style="font-family:arial;">BlogCastRepository.com</span><br /><span style="font-family:Arial;"></span></a><br /><span style="font-family:Arial;"></span></p>
<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/92377218009570869-2817311873342195238?l=bassplayerdoc.blogspot.com' alt='' /></div>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/merge-statement-in-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">29</post-id>	</item>
	</channel>
</rss>