<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'Merge' and 'Bugs'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Merge,Bugs&amp;orTags=0</link><description>Search results matching tags 'Merge' and 'Bugs'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Incorrect Results with Indexed Views</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/02/05/incorrect-results-with-indexed-views.aspx</link><pubDate>Tue, 05 Feb 2013 22:18:11 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47500</guid><dc:creator>Paul White</dc:creator><description>&lt;hr /&gt;&lt;font size="3" face="Calibri"&gt;&lt;em&gt;Summary: If you use MERGE, indexed views and foreign keys, your queries can return incorrect results.&lt;/em&gt;&lt;/font&gt;   &lt;br /&gt;  &lt;hr /&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;Microsoft have &lt;a href="http://support.microsoft.com/kb/2756471" target="_blank"&gt;released a fix&lt;/a&gt; for incorrect results returned when querying an indexed view. &lt;/font&gt;&lt;font size="3" face="Calibri"&gt;The problem applies to:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;SQL Server 2012&lt;/strong&gt;&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;SQL Server 2008 R2&lt;/strong&gt;&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;SQL Server 2008&lt;/strong&gt;&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;The &lt;a href="http://support.microsoft.com/kb/2756471" target="_blank"&gt;Knowledge Base article&lt;/a&gt; does not go into much detail, or provide a reproduction script, but this blog entry does :)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;The KB does say that reproducing the bug requires these features:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="3" face="Calibri"&gt;An indexed view on two tables that have a foreign key relationship&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;An update performed against the base tables&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;A query executed against the indexed view using a NOEXPAND hint&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;There are two important details I would like to add right away:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/ms181714.aspx" target="_blank"&gt;NOEXPAND&lt;/a&gt; hint is &lt;strong&gt;not required&lt;/strong&gt; to reproduce the bug on Enterprise Edition&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The update must be performed by a &lt;a href="http://technet.microsoft.com/en-us/library/bb510625.aspx" target="_blank"&gt;MERGE&lt;/a&gt; statement&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;The fix is available in the following cumulative update packages:&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2790947" target="_blank"&gt;Cumulative Update 2 for SQL Server 2012 SP1&lt;/a&gt; [build 11.0.3339]         &lt;br /&gt;&lt;/font&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2777772" target="_blank"&gt;Cumulative Update 5 for SQL Server 2012 RTM&lt;/a&gt; [build 11.0.2395]         &lt;br /&gt;&lt;/font&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2777358" target="_blank"&gt;Cumulative Update 4 for SQL Server 2008 R2 SP2&lt;/a&gt; [build 10.50.4270]         &lt;br /&gt;&lt;/font&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2783135" target="_blank"&gt;Cumulative Update 10 for SQL Server 2008 R2 SP1&lt;/a&gt; [build 10.50.2868]         &lt;br /&gt;&lt;/font&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2771833" target="_blank"&gt;Cumulative Update 8 for SQL Server 2008 SP3&lt;/a&gt; [build 10.00.5828]&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;No service pack contains this fix, you must apply one of the hotfix packages above.&lt;/font&gt;&lt;/p&gt;  &lt;h2&gt;Steps to Reproduce&lt;/h2&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;The first thing we will need is two tables:&lt;/font&gt;&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:300px;width:97.5%;background-color:#f4f4f4;"&gt;   &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;     &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt; dbo.Parent &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;(&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    parent_id &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;IDENTITY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(20) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt; PK_Parent_id &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt; (parent_id)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt; dbo.Child&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;(&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    child_id &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;IDENTITY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    parent_id &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt; PK_Child_id &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt; (child_id)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;And a few rows of data:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;INSERT dbo.Child (parent_id)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt;.parent_id &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    (&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    INSERT Parent &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;OUTPUT&lt;/span&gt; inserted.parent_id &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;        (&lt;span style="color:#006080;"&gt;'Apple'&lt;/span&gt;), &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;        (&lt;span style="color:#006080;"&gt;'Banana'&lt;/span&gt;), &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;        (&lt;span style="color:#006080;"&gt;'Cherry'&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    ) &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The tables now look like this (parent first):&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_796FB1B9.png" target="_blank"&gt;&lt;img title="Original Data" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Original Data" src="http://sqlblog.com/blogs/paul_white/image_thumb_3141F5DD.png" width="168" height="203" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can now add the required &lt;strong&gt;FOREIGN KEY&lt;/strong&gt; relationship:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;ALTER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt; dbo.Child&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;ADD&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt; FK_Child_Parent&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;FOREIGN&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt; (parent_id)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;REFERENCES&lt;/span&gt; dbo.Parent (parent_id);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Next, a very simple &lt;strong&gt;indexed view&lt;/strong&gt; that joins the two tables (the view could contain more complex features like aggregates):&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:300px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;VIEW&lt;/span&gt; dbo.ParentsAndChildren&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; SCHEMABINDING &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    p.parent_id, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    p.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    c.child_id&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.Parent &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; p&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; dbo.Child &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    c.parent_id = p.parent_id;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;UNIQUE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;INDEX&lt;/span&gt; cuq &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; dbo.ParentsAndChildren (child_id);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The final step is to use a MERGE statement to make some changes to the Parent table:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:400px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DECLARE&lt;/span&gt; @ParentMerge &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;(&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    parent_id &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(20) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;INSERT @ParentMerge&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    (parent_id, &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    (1, &lt;span style="color:#006080;"&gt;'Kiwi Fruit'&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    (4, &lt;span style="color:#006080;"&gt;'Dragon Fruit'&lt;/span&gt;);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;MERGE dbo.Parent &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; p&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;USING&lt;/span&gt; @ParentMerge &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; s &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    s.parent_id = p.parent_id&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;UPDATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; = s.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    INSERT (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; (s.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;OUTPUT&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    $&lt;span style="color:#0000ff;"&gt;action&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    inserted.parent_id, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    deleted.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; old_value, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    inserted.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; new_value;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;This MERGE performs two actions:&lt;/font&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;Updates the value column of parent row 1 from Apple to Kiwi Fruit&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;Adds a new parent row 4 for Dragon Fruit&lt;/font&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The statement includes an &lt;a href="http://msdn.microsoft.com/en-us/library/ms177564.aspx" target="_blank"&gt;OUTPUT&lt;/a&gt; clause to show the changes it makes (this is not required for the repro):&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_3634B15D.png" target="_blank"&gt;&lt;img title="MERGE changes" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="MERGE changes" src="http://sqlblog.com/blogs/paul_white/image_thumb_5C2A61B3.png" width="306" height="93" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;This confirms that the changes have been made as we requested: parent row 1 has changed; and row 4 has been added. The changes are reflected in the base tables:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.Parent &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; p;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.Child &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_5432BF51.png" target="_blank"&gt;&lt;img title="Updated Base Table Data" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Updated Base Table Data" src="http://sqlblog.com/blogs/paul_white/image_thumb_6536ED34.png" width="187" height="224" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;As highlighted, row 1 has changed from &lt;em&gt;Apple&lt;/em&gt; to &lt;em&gt;Kiwi Fruit&lt;/em&gt; and row 4 has been added.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;We do &lt;em&gt;not&lt;/em&gt; expect to see row 4 in the indexed view because there are no child records for that row, and the indexed view uses an &lt;em&gt;inner&lt;/em&gt; join. Checking the indexed view using the NOEXPAND table hint (required in non-Enterprise SKUs to use indexes on a view):&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; (NOEXPAND);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_2ADB7753.png" target="_blank"&gt;&lt;img title="Incorrect indexed view data" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Incorrect indexed view data" src="http://sqlblog.com/blogs/paul_white/image_thumb_22E3D4F1.png" width="217" height="111" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;The results are incorrect.&lt;/strong&gt; They show the old value of the data for parent row 1.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Now we try using the &lt;a href="http://msdn.microsoft.com/en-us/library/ms181714.aspx" target="_blank"&gt;EXPAND VIEWS&lt;/a&gt; query hint to force SQL Server to access the base tables rather than reading view indexes:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;OPTION&lt;/span&gt; (EXPAND VIEWS);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_48D98547.png" target="_blank"&gt;&lt;img title="Expand Views Hint" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Expand Views Hint" src="http://sqlblog.com/blogs/paul_white/image_thumb_40E1E2E5.png" width="223" height="113" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;This query produces correct results&lt;/strong&gt;.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;On SQL Server Enterprise Edition, the optimizer chooses whether to access the indexed view or the base tables. For following query, without any hints, the optimizer chooses not to expand the view. It reads the view index and produces &lt;strong&gt;incorrect&lt;/strong&gt; results:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#008000;"&gt;-- Enterprise Edition ONLY&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_14C4E5F4.png" target="_blank"&gt;&lt;img title="Indexed View Matching" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Indexed View Matching" src="http://sqlblog.com/blogs/paul_white/image_thumb_0CCD4392.png" width="217" height="111" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Perhaps adding a child row to match the new parent row 4 will somehow fix things up?&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;INSERT dbo.Child (parent_id) &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; (4);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; (NOEXPAND);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren &lt;span style="color:#0000ff;"&gt;OPTION&lt;/span&gt; (EXPAND VIEWS);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_32C2F3E8.png" target="_blank"&gt;&lt;img title="After Insert" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="After Insert" src="http://sqlblog.com/blogs/paul_white/image_thumb_71B47483.png" width="239" height="248" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;No. The query plan that accesses the view index still returns an incorrect value for row 1. &lt;strong&gt;It seems MERGE has corrupted our indexed view&lt;/strong&gt;.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;h3&gt;Analysis using DBCC CHECKTABLE&lt;/h3&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Checking the view with &lt;a href="http://msdn.microsoft.com/en-us/library/ms174338.aspx" target="_blank"&gt;DBCC CHECKTABLE&lt;/a&gt; returns no errors:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DBCC&lt;/span&gt; CHECKTABLE (ParentsAndChildren);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_4935926F.png" target="_blank"&gt;&lt;img title="DBCC output 1" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="DBCC output 1" src="http://sqlblog.com/blogs/paul_white/image_thumb_6F2B42C5.png" width="644" height="83" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Unless we use the &lt;strong&gt;EXTENDED_LOGICAL_CHECKS&lt;/strong&gt; option:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DBCC&lt;/span&gt; CHECKTABLE (ParentsAndChildren) &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; EXTENDED_LOGICAL_CHECKS;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_7837CE46.png" target="_blank"&gt;&lt;img title="DBCC output 2" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="DBCC output 2" src="http://sqlblog.com/blogs/paul_white/image_thumb_6B5D7828.png" width="644" height="217" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The damage is repairable:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;ALTER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;DATABASE&lt;/span&gt; Sandpit &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; SINGLE_USER &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;ROLLBACK&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;IMMEDIATE&lt;/span&gt;;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DBCC&lt;/span&gt; CHECKTABLE (ParentsAndChildren, REPAIR_REBUILD) &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; EXTENDED_LOGICAL_CHECKS;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DBCC&lt;/span&gt; CHECKTABLE (ParentsAndChildren) &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; EXTENDED_LOGICAL_CHECKS;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;ALTER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;DATABASE&lt;/span&gt; Sandpit &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; MULTI_USER;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_191B1B21.png" target="_blank"&gt;&lt;img title="DBCC repair" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="DBCC repair" src="http://sqlblog.com/blogs/paul_white/image_thumb_3DCC3298.png" width="644" height="277" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;You probably do not want to set your database to SINGLE_USER mode and run a DBCC repair after every MERGE statement, however. We could also rebuild the indexed view’s clustered index manually, of course.&lt;/font&gt;&lt;/p&gt;

&lt;h2&gt;Cause&lt;/h2&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;For the MERGE statement above, the query optimizer builds a plan that &lt;strong&gt;does not update the indexed view&lt;/strong&gt; (click to enlarge):&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_072E6187.png" target="_blank"&gt;&lt;img title="Incorrect Update Plan" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Incorrect Update Plan" src="http://sqlblog.com/blogs/paul_white/image_thumb_6A64C6A4.png" width="660" height="103" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;In a version of SQL Server with the fix applied, the same MERGE statement produces a plan that does maintain the indexed view:&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_73715225.png" target="_blank"&gt;&lt;img title="Correct Update Plan" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Correct Update Plan" src="http://sqlblog.com/blogs/paul_white/image_thumb_049509FC.png" width="644" height="101" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The plan operators used to keep the view index in step with the base tables are highlighted. Without these operators, the changes to the base table are not correctly written to any indexes defined on the view. The &lt;strong&gt;root cause&lt;/strong&gt; is related to the same simplification that allows the optimizer to remove the reference to the Parent table in this query:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    COUNT_BIG(*)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.Parent &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; p &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; dbo.Child &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    c.parent_id = p.parent_id;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_0DA1957D.png" target="_blank"&gt;&lt;img title="Simplification Example Plan" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Simplification Example Plan" src="http://sqlblog.com/blogs/paul_white/image_thumb_0C5CFC9E.png" width="314" height="87" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The FOREIGN KEY relationship and NOT NULL constraints on the referencing column together mean that the join to Parent cannot affect the result of the query, so the join &lt;a href="http://msmvps.com/blogs/robfarley/archive/2008/11/09/join-simplification-in-sql-server.aspx" target="_blank"&gt;is simplified away&lt;/a&gt;. In SQL Server 2012, we can see when this simplification is performed because the following message appears when undocumented trace flags 8619 and 3604 are enabled during compilation:&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_2009E632.png" target="_blank"&gt;&lt;img title="Trace Flag 8619 output" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Trace Flag 8619 output" src="http://sqlblog.com/blogs/paul_white/image_thumb_2A5B0A92.png" width="457" height="37" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The same message is emitted when a MERGE statement contains a WHEN MATCHED THEN UPDATE clause and either a WHEN NOT MATCHED THEN INSERT or WHEN MATCHED … THEN DELETE clause. These conditions combine such that the optimizer incorrectly concludes that a table reference can be removed, when in fact it is needed later on when the update side of the plan is built.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Other details of the query and database can affect whether the simplification can be misapplied. For example, if the FOREIGN KEY constraint contains an ON DELETE CASCADE clause, and the MERGE contains a DELETE clause, the simplification is not performed, the TF 8619 message does not appear, and the bug does not manifest.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The key to determining whether a particular query is vulnerable to this bug (TF 8619 aside) is to check whether the query plan includes operators to maintain the indexed view. At a minimum, you should see a update operator for the view:&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_22636830.png" target="_blank"&gt;&lt;img title="View Update Operator" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="View Update Operator" src="http://sqlblog.com/blogs/paul_white/image_thumb_48591886.png" width="183" height="107" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;SQL Sentry &lt;a href="http://www.sqlsentry.net/plan-explorer/sql-server-query-view.asp" target="_blank"&gt;Plan Explorer&lt;/a&gt; identifies the operator as applying to a view explicitly, in SSMS you need to click on the graphical operator and inspect the Properties window.&lt;/font&gt;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The updated conditions for incorrect results are:&lt;/font&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;An &lt;strong&gt;indexed view&lt;/strong&gt; that joins tables&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;Two of the tables have a single-column &lt;strong&gt;FOREIGN KEY constraint&lt;/strong&gt;&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;A &lt;strong&gt;MERGE&lt;/strong&gt; statement contains an &lt;strong&gt;UPDATE&lt;/strong&gt; action that affects one of the tables&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The &lt;strong&gt;MERGE&lt;/strong&gt; statement also contains an &lt;strong&gt;INSERT or DELETE&lt;/strong&gt; action (or both)&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The optimizer applies a &lt;strong&gt;simplification&lt;/strong&gt; that removes a table reference based on the FK relationship and other metadata&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;As a result, the MERGE execution plan does not contain the operators necessary to correctly maintain the indexed view&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;A subsequent query plan accesses an index on the view, either explicitly or via &lt;strong&gt;indexed-view matching&lt;/strong&gt; (Enterprise Edition)&lt;/font&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The simplification is not applied in &lt;em&gt;tempdb&lt;/em&gt;&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The simplification is not applied to multi-column foreign key constraints&lt;/font&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Under these conditions, &lt;strong&gt;the indexes on the view do not reflect the state of the base tables and incorrect results are returned&lt;/strong&gt;. Once the hot fix is applied, the optimizer does not misapply the simplification so the correct indexed view maintenance features are built into execution plans.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;Update:&lt;/strong&gt; I am adding this based on Ian Yates’ great question in the comments: my expectation is that &lt;strong&gt;applying this hotfix will not remove any existing indexed view corruption&lt;/strong&gt;. You would need to test the hotfix as usual, apply it, and then either rebuild all affected indexed views manually, or run DBCC CHECKDB with the EXTENDED_LOGICAL_CHECKS option (which could take a long time).&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;© 2013 Paul White – All Rights Reserved&lt;/font&gt;&lt;/p&gt;

&lt;p align="left"&gt;&lt;a title="SQL Intersection" href="http://www.sqlintersection.com/" target="_blank"&gt;&lt;img title="Ill_Be_There4" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Ill_Be_There4" src="http://sqlblog.com/blogs/paul_white/Ill_Be_There4_0D1F26E1.jpg" width="140" height="110" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Twitter: &lt;a href="http://twitter.com/SQL_Kiwi"&gt;@SQL_Kiwi&lt;/a&gt; 

    &lt;br /&gt;Email: &lt;a href="mailto:SQLKiwi@gmail.com"&gt;SQLKiwi@gmail.com&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;</description></item><item><title>MERGE Bug with Filtered Indexes</title><link>http://sqlblog.com/blogs/paul_white/archive/2012/12/09/merge-bug-with-filtered-indexes.aspx</link><pubDate>Sun, 09 Dec 2012 17:54:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:46572</guid><dc:creator>Paul White</dc:creator><description>&lt;p align="left"&gt;A MERGE statement can fail, and incorrectly report a unique key violation when:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div align="left"&gt;The target table uses a unique filtered index; and&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="left"&gt;No key column of the filtered index is updated; and&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="left"&gt;A column from the filtering condition is updated; and&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="left"&gt;Transient key violations are possible&lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;h3 align="left"&gt;Example Tables&lt;/h3&gt;  &lt;p align="left"&gt;Say we have two tables, one that is the target of a MERGE statement, and another that contains updates to be applied to the target.&amp;#160; The target table contains three columns, an integer primary key, a single character alternate key, and a status code column.&amp;#160; A filtered unique index exists on the alternate key, but is only enforced where the status code is ‘a’:&lt;/p&gt;  &lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:300px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt; #Target &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;(&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    pk          &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ak          &lt;span style="color:#0000ff;"&gt;character&lt;/span&gt;(1) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    status_code &lt;span style="color:#0000ff;"&gt;character&lt;/span&gt;(1) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt; (pk)&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;);&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;UNIQUE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;INDEX&lt;/span&gt; uq1&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; #Target (ak)&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;INCLUDE&lt;/span&gt; (status_code)&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; status_code = &lt;span style="color:#006080;"&gt;'a'&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p align="left"&gt;The changes table contains just an integer primary key (to identify the target row to change) and the new status code:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt; #Changes&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;(&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    pk          &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    status_code &lt;span style="color:#0000ff;"&gt;character&lt;/span&gt;(1) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt; (pk)&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;h3 align="left"&gt;Sample Data&lt;/h3&gt;

&lt;p align="left"&gt;The sample data for the example is:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;INSERT #Target &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (pk, ak, status_code)&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (1, &lt;span style="color:#006080;"&gt;'A'&lt;/span&gt;, &lt;span style="color:#006080;"&gt;'a'&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (2, &lt;span style="color:#006080;"&gt;'B'&lt;/span&gt;, &lt;span style="color:#006080;"&gt;'a'&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (3, &lt;span style="color:#006080;"&gt;'C'&lt;/span&gt;, &lt;span style="color:#006080;"&gt;'a'&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (4, &lt;span style="color:#006080;"&gt;'A'&lt;/span&gt;, &lt;span style="color:#006080;"&gt;'d'&lt;/span&gt;);&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;INSERT #Changes&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (pk, status_code)&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (1, &lt;span style="color:#006080;"&gt;'d'&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (4, &lt;span style="color:#006080;"&gt;'a'&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;b&gt;Target&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Changes&lt;/b&gt; 

    &lt;br /&gt;╔════╦════╦═════════════╗&amp;#160;&amp;#160;&amp;#160; ╔════╦═════════════╗ 

    &lt;br /&gt;║ pk ║ ak ║ status_code ║&amp;#160;&amp;#160;&amp;#160; ║ pk ║ status_code ║ 

    &lt;br /&gt;╠════╬════╬═════════════╣&amp;#160;&amp;#160;&amp;#160; ╠════╬═════════════╣ 

    &lt;br /&gt;║&amp;#160; 1 ║ A&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 1 ║ d&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 2 ║ B&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 4 ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 3 ║ C&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ╚════╩═════════════╝ 

    &lt;br /&gt;║&amp;#160; 4 ║ A&amp;#160; ║ d&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;╚════╩════╩═════════════╝&lt;/font&gt; 

  &lt;br /&gt;&lt;/p&gt;

&lt;p align="left"&gt;The target table’s alternate key (ak) column is unique, for rows where status_code = ‘a’.&amp;#160; Applying the changes to the target will change row 1 from status ‘a’ to status ‘d’, and row 4 from status ‘d’ to status ‘a’.&amp;#160; The result of applying all the changes will still satisfy the filtered unique index, because the ‘A’ in row 1 will be deleted from the index and the ‘A’ in row 4 will be added.&lt;/p&gt;

&lt;h3 align="left"&gt;Merge Test One&lt;/h3&gt;

&lt;p align="left"&gt;Let’s now execute a MERGE statement to apply the changes:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;MERGE #Target &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; t&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;USING&lt;/span&gt; #Changes &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    c.pk = t.pk&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; MATCHED &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; c.status_code &amp;lt;&amp;gt; t.status_code &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;UPDATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; status_code = c.status_code;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p align="left"&gt;The MERGE changes the two target rows as expected.&amp;#160; The updated target table now contains:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;╔════╦════╦═════════════╗ 
    &lt;br /&gt;║ pk ║ ak ║ status_code ║ 

    &lt;br /&gt;╠════╬════╬═════════════╣ 

    &lt;br /&gt;║&amp;#160; 1 ║ A&amp;#160; ║ &lt;b&gt;&lt;font color="#ff0000"&gt;d&lt;/font&gt;&lt;/b&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ &amp;lt;—changed from ‘a’ 

    &lt;br /&gt;║&amp;#160; 2 ║ B&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 3 ║ C&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 4 ║ A&amp;#160; ║ &lt;b&gt;&lt;font color="#ff0000"&gt;a&lt;/font&gt;&lt;/b&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ &amp;lt;—changed from ‘d’ 

    &lt;br /&gt;╚════╩════╩═════════════╝&lt;/font&gt;&lt;/p&gt;

&lt;h3 align="left"&gt;Merge Test Two&lt;/h3&gt;

&lt;p align="left"&gt;Now let’s repopulate the changes table to reverse the updates we just performed:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;TRUNCATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt; #Changes;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;INSERT #Changes&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (pk, status_code)&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (1, &lt;span style="color:#006080;"&gt;'a'&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    (4, &lt;span style="color:#006080;"&gt;'d'&lt;/span&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p align="left"&gt;This will change row 1 back to status ‘a’ and row 4 back to status ‘d’.&amp;#160; As a reminder, the current state of the tables is:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;b&gt;Target&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Changes&lt;/b&gt; 

    &lt;br /&gt;╔════╦════╦═════════════╗&amp;#160;&amp;#160;&amp;#160; ╔════╦═════════════╗ 

    &lt;br /&gt;║ pk ║ ak ║ status_code ║&amp;#160;&amp;#160;&amp;#160; ║ pk ║ status_code ║ 

    &lt;br /&gt;╠════╬════╬═════════════╣&amp;#160;&amp;#160;&amp;#160; ╠════╬═════════════╣ 

    &lt;br /&gt;║&amp;#160; 1 ║ A&amp;#160; ║ d&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 1 ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 2 ║ B&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 4 ║ d&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 3 ║ C&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ╚════╩═════════════╝ 

    &lt;br /&gt;║&amp;#160; 4 ║ A&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;╚════╩════╩═════════════╝&lt;/font&gt; 

  &lt;br /&gt;&lt;/p&gt;

&lt;p align="left"&gt;We execute the same MERGE statement:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;MERGE #Target &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; t&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;USING&lt;/span&gt; #Changes &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    c.pk = t.pk&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; MATCHED &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; c.status_code &amp;lt;&amp;gt; t.status_code &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;UPDATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; status_code = c.status_code;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p align="left"&gt;However this time we receive the following message:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font face="Courier New"&gt;&lt;font color="#ff0000"&gt;Msg 2601, Level 14, State 1, Line 1 
        &lt;br /&gt;Cannot insert duplicate key row in object 'dbo.#Target' with unique index 'uq1'. The duplicate key value is (A). 

        &lt;br /&gt;&lt;/font&gt;The statement has been terminated.&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Applying the changes using UPDATE&lt;/h3&gt;

&lt;p align="left"&gt;Let’s now rewrite the MERGE to use UPDATE instead:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;UPDATE&lt;/span&gt; t&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; status_code = c.status_code&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; #Target &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; t&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; #Changes &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    t.pk = c.pk&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    c.status_code &amp;lt;&amp;gt; t.status_code;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This query succeeds where the MERGE failed.&amp;#160; The two rows are updated as expected:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;╔════╦════╦═════════════╗ 
    &lt;br /&gt;║ pk ║ ak ║ status_code ║ 

    &lt;br /&gt;╠════╬════╬═════════════╣ 

    &lt;br /&gt;║&amp;#160; 1 ║ A&amp;#160; ║ &lt;b&gt;&lt;font color="#ff0000"&gt;a&lt;/font&gt;&lt;/b&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ &amp;lt;—changed back to ‘a’ 

    &lt;br /&gt;║&amp;#160; 2 ║ B&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 3 ║ C&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 4 ║ A&amp;#160; ║ &lt;b&gt;&lt;font color="#ff0000"&gt;d&lt;/font&gt;&lt;/b&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ &amp;lt;—changed back to ‘d’ 

    &lt;br /&gt;╚════╩════╩═════════════╝&lt;/font&gt;&lt;/p&gt;

&lt;h3&gt;What went wrong with the MERGE?&lt;/h3&gt;

&lt;p align="left"&gt;In this test, the MERGE query execution happens to apply the changes in the order of the ‘pk’ column.&lt;/p&gt;

&lt;p align="left"&gt;&lt;b&gt;In test one, this was not a problem&lt;/b&gt;: row 1 is removed from the unique filtered index by changing status_code from ‘a’ to ‘d’ before row 4 is added.&amp;#160; At no point does the table contain two rows where ak = ‘A’ and status_code = ‘a’.&lt;/p&gt;

&lt;p align="left"&gt;&lt;b&gt;In test two, however&lt;/b&gt;, the first change was to change row 1 from status ‘d’ to status ‘a’.&amp;#160; This change means there would be two rows in the filtered unique index where ak = ‘A’ (both row 1 and row 4 meet the index filtering criteria ‘status_code = a’).&lt;/p&gt;

&lt;p align="left"&gt;The storage engine does not allow the query processor to violate a unique key (unless IGNORE_DUP_KEY is ON, but that is a different story, and doesn’t apply to MERGE in any case).&amp;#160; This strict rule applies regardless of the fact that if &lt;b&gt;all&lt;/b&gt; changes were applied, there would be no unique key violation (row 4 would eventually be changed from ‘a’ to ‘d’, removing it from the filtered unique index, and resolving the key violation).&lt;/p&gt;

&lt;h3 align="left"&gt;Why it went wrong&lt;/h3&gt;

&lt;p align="left"&gt;The query optimizer usually detects when this sort of &lt;b&gt;temporary uniqueness violation&lt;/b&gt; could occur, and builds a plan that avoids the issue.&amp;#160; I wrote about this a couple of years ago in my post &lt;a href="http://bit.ly/SneakyReads" target="_blank"&gt;Beware Sneaky Reads with Unique Indexes&lt;/a&gt; (you can read more about the details on pages 495-497 of &lt;a href="http://www.microsoft.com/learning/en/us/book.aspx?id=12967" target="_blank"&gt;Microsoft SQL Server 2008 Internals&lt;/a&gt; or in Craig Freedman’s &lt;a href="http://blogs.msdn.com/b/craigfr/archive/2007/09/06/maintaining-unique-indexes.aspx" target="_blank"&gt;blog post&lt;/a&gt; on maintaining unique indexes).&amp;#160; To summarize though, the optimizer introduces &lt;b&gt;Split, Filter, Sort, and Collapse&lt;/b&gt; operators into the query plan to:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;div align="left"&gt;&lt;b&gt;Split&lt;/b&gt; each row update into delete followed by an inserts&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;div align="left"&gt;&lt;b&gt;Filter&lt;/b&gt; out rows that would not change the index (due to the filter on the index, or a &lt;a href="http://bit.ly/NonUpdatingUpdates" target="_blank"&gt;non-updating update&lt;/a&gt;)&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;div align="left"&gt;&lt;b&gt;Sort&lt;/b&gt; the resulting stream by index key, with deletes before inserts&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;div align="left"&gt;&lt;b&gt;Collapse&lt;/b&gt; delete/insert pairs on the same index key back into an update&lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p align="left"&gt;The effect of all this is that &lt;b&gt;only net changes are applied&lt;/b&gt; to an index (as one or more insert, update, and/or delete operations).&amp;#160; In this case, the net effect is a single update of the filtered unique index: changing the row for ak = ‘A’ from pk = 4 to pk = 1.&amp;#160; In case that is less than 100% clear, let’s look at the operation in test two again:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;b&gt;Target&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Changes&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Result&lt;/b&gt; 

    &lt;br /&gt;╔════╦════╦═════════════╗&amp;#160;&amp;#160;&amp;#160; ╔════╦═════════════╗&amp;#160;&amp;#160;&amp;#160; ╔════╦════╦═════════════╗ 

    &lt;br /&gt;║ pk ║ ak ║ status_code ║&amp;#160;&amp;#160;&amp;#160; ║ pk ║ status_code ║&amp;#160;&amp;#160;&amp;#160; ║ pk ║ ak ║ status_code ║ 

    &lt;br /&gt;╠════╬════╬═════════════╣&amp;#160;&amp;#160;&amp;#160; ╠════╬═════════════╣&amp;#160;&amp;#160;&amp;#160; ╠════╬════╬═════════════╣ 

    &lt;br /&gt;║&amp;#160; 1 ║ A&amp;#160; ║ d&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 1 ║ d&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 1 ║ A&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 2 ║ B&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 4 ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 2 ║ B&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 3 ║ C&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ╚════╩═════════════╝&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 3 ║ C&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 4 ║ A&amp;#160; ║ a&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 4 ║ A&amp;#160; ║ d&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ║ 

    &lt;br /&gt;╚════╩════╩═════════════╝&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ╚════╩════╩═════════════╝&lt;/font&gt; 

  &lt;br /&gt;&lt;/p&gt;

&lt;p align="left"&gt;From the &lt;b&gt;filtered index’s point of view&lt;/b&gt; (filtered for status_code = ‘a’ and shown in nonclustered index key order) the overall effect of the query is:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;&lt;b&gt;&amp;#160; Before&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; After 
      &lt;br /&gt;&lt;/b&gt;╔════╦════╗&amp;#160;&amp;#160;&amp;#160; ╔════╦════╗ 

    &lt;br /&gt;║ pk ║ ak ║&amp;#160;&amp;#160;&amp;#160; ║ pk ║ ak ║ 

    &lt;br /&gt;╠════╬════╣&amp;#160;&amp;#160;&amp;#160; ╠════╬════╣ 

    &lt;br /&gt;║&amp;#160; &lt;font color="#ff0000"&gt;&lt;b&gt;4&lt;/b&gt;&lt;/font&gt; ║ A&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; &lt;b&gt;&lt;font color="#ff0000"&gt;1&lt;/font&gt;&lt;/b&gt; ║ A&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 2 ║ B&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 2 ║ B&amp;#160; ║ 

    &lt;br /&gt;║&amp;#160; 3 ║ C&amp;#160; ║&amp;#160;&amp;#160;&amp;#160; ║&amp;#160; 3 ║ C&amp;#160; ║ 

    &lt;br /&gt;╚════╩════╝&amp;#160;&amp;#160;&amp;#160; ╚════╩════╝&lt;/font&gt; 

  &lt;br /&gt;&lt;/p&gt;

&lt;p align="left"&gt;The &lt;b&gt;single net change&lt;/b&gt; there is a change of pk from 4 to 1 for the nonclustered index entry ak = ‘A’.&amp;#160; This is the magic performed by the split, sort, and collapse.&amp;#160; Notice in particular how the original changes to the index key (on the ‘ak’ column) have been transformed into an update of a non-key column (pk is included in the nonclustered index).&amp;#160; By not updating any nonclustered index keys, we are &lt;b&gt;guaranteed to avoid transient key violations&lt;/b&gt;.&lt;/p&gt;

&lt;h3 align="left"&gt;The Execution Plans&lt;/h3&gt;

&lt;p align="left"&gt;The estimated MERGE execution plan that produces the &lt;b&gt;incorrect key-violation error&lt;/b&gt; looks like this (click to enlarge in a new window):&lt;/p&gt;

&lt;p align="left"&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_0C120630.png" target="_blank"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MERGE plan" border="0" alt="MERGE plan" src="http://sqlblog.com/blogs/paul_white/image_thumb_7BB68E76.png" width="660" height="142" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p align="left"&gt;The successful UPDATE execution plan is (click to enlarge in a new window):&lt;/p&gt;

&lt;p align="left"&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_58A61D06.png" target="_blank"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="UPDATE execution plan" border="0" alt="UPDATE execution plan" src="http://sqlblog.com/blogs/paul_white/image_thumb_15E6D1CE.png" width="660" height="111" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p align="left"&gt;The MERGE execution plan is a &lt;b&gt;narrow&lt;/b&gt; (per-row) update.&amp;#160; The single Clustered Index Merge operator maintains both the clustered index and the filtered nonclustered index.&amp;#160; The UPDATE plan is a &lt;b&gt;wide&lt;/b&gt; (per-index) update.&amp;#160; The clustered index is maintained first, then the Split, Filter, Sort, Collapse sequence is applied before the nonclustered index is separately maintained.&lt;/p&gt;

&lt;p align="left"&gt;&lt;b&gt;There is always a wide update plan&lt;/b&gt; for any query that modifies the database. The narrow form is a performance optimization where the number of rows is expected to be relatively small, and is not available for all operations.&amp;#160; One of the operations that &lt;b&gt;should disallow a narrow plan&lt;/b&gt; is maintaining a unique index where intermediate key violations could occur.&lt;/p&gt;

&lt;h3 align="left"&gt;Workarounds&lt;/h3&gt;

&lt;p align="left"&gt;The MERGE can be made to work (producing a wide update plan with split, sort, and collapse) by:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;div align="left"&gt;Adding all columns referenced in the filtered index’s WHERE clause to the index key (INCLUDE is not sufficient); or&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;div align="left"&gt;Executing the query with trace flag 8790 set e.g. OPTION (QUERYTRACEON 8790).&lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p align="left"&gt;Undocumented trace flag 8790 forces a wide update plan for any data-changing query (remember that a wide update plan is always possible).&amp;#160; Either change will produce a successfully-executing wide update plan for the MERGE that failed previously.&lt;/p&gt;

&lt;h3 align="left"&gt;Conclusion&lt;/h3&gt;

&lt;p align="left"&gt;The optimizer fails to spot the possibility of transient unique key violations with MERGE under the conditions listed at the start of this post.&amp;#160; It incorrectly chooses a narrow plan for the MERGE, which cannot provide the protection of a split/sort/collapse sequence for the nonclustered index maintenance.&lt;/p&gt;

&lt;p align="left"&gt;The MERGE plan may fail at execution time depending on the order in which rows are processed, and the distribution of data in the database.&amp;#160; Worse, a previously solid MERGE query may &lt;b&gt;suddenly start to fail unpredictably&lt;/b&gt; if a filtered unique index is added to the merge target table at any point.&lt;/p&gt;

&lt;p align="left"&gt;&lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/773895/merge-incorrectly-reports-unique-key-violations" target="_blank"&gt;Connect bug filed here&lt;/a&gt;&lt;/p&gt;

&lt;p align="left"&gt;&lt;i&gt;Bug reproduced on the following SQL Server versions (all x64 Developer Edition):&lt;/i&gt;&lt;/p&gt;

&lt;p align="left"&gt;&lt;i&gt;&lt;strong&gt;2012 SP1 CUI&lt;/strong&gt; (build 11.0.3321 – November 2012)

    &lt;br /&gt;&lt;strong&gt;2008 R2 SP2 CU3&lt;/strong&gt; (build 10.50.4266 – October 2012)

    &lt;br /&gt;&lt;strong&gt;2008 SP3 CU8 &lt;/strong&gt;(build 10.0.5828 – November 2012)&lt;/i&gt;&lt;/p&gt;

&lt;p align="left"&gt;© 2012 Paul White – All Rights Reserved&lt;/p&gt;

&lt;p align="left"&gt;Twitter: &lt;a href="http://twitter.com/SQL_Kiwi"&gt;@SQL_Kiwi&lt;/a&gt; 

  &lt;br /&gt;Email: &lt;a href="mailto:SQLkiwi@gmail.com"&gt;SQLkiwi@gmail.com&lt;/a&gt;&lt;/p&gt;</description></item><item><title>An Interesting MERGE Bug</title><link>http://sqlblog.com/blogs/paul_white/archive/2010/08/03/another-interesting-merge-bug.aspx</link><pubDate>Tue, 03 Aug 2010 17:30:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:27573</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;&lt;i&gt;Summary: Investigating an optimiser transformation that exposes a bug in SQL Server’s MERGE implementation.&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;I came across a &lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/581548/sql2008-r2-merge-statement-with-only-table-variables-fails"&gt;Connect item&lt;/a&gt; today (by fellow SQL Server Central member ‘ALZDBA’) describing how using a combination of relatively new features can produce incorrect results or even an access violation inside SQL Server.&lt;/p&gt;  &lt;h4&gt;Reproducing the bug&lt;/h4&gt;  &lt;p&gt;We’ll look first at how MERGE can produce incorrect an output.&amp;#160; We’ll need two tables: one that contains two rows of existing data; and a second that contains four rows of change information.&amp;#160; The overall process looks like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/MergeOverview_25CC1C68.png"&gt;&lt;img style="border-right-width:0px;display:block;float:none;border-top-width:0px;border-bottom-width:0px;margin-left:auto;border-left-width:0px;margin-right:auto;" title="Merge-Overview" border="0" alt="Merge-Overview" src="http://sqlblog.com/blogs/paul_white/MergeOverview_thumb_36D56BAD.png" width="627" height="177" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This is a classic MERGE (or “upsert”) requirement.&amp;#160; If the row already exists, the new value is added to it, otherwise a new row is inserted.&amp;#160; Here’s the code to generate the sample tables and data:&lt;/p&gt;  &lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;&lt;span style="color:#0000ff;"&gt;DECLARE&lt;/span&gt; @Target&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt;   (&lt;br /&gt;        row_id  &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;   &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;&lt;br /&gt;        );&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;DECLARE&lt;/span&gt; @Delta&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt;   (&lt;br /&gt;        row_id  &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt;,&lt;br /&gt;        delta   &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;&lt;br /&gt;        );&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#008000;"&gt;-- Existing records (1 &amp;amp; 3)&lt;/span&gt;&lt;br /&gt;INSERT  @Target&lt;br /&gt;        (row_id, &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt;  (1, 1),&lt;br /&gt;        (3, 3);&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#008000;"&gt;-- Change table:&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#008000;"&gt;-- Updates rows 1 &amp;amp; 3&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#008000;"&gt;-- Inserts rows 2 &amp;amp; 4&lt;/span&gt;&lt;br /&gt;INSERT  @Delta&lt;br /&gt;        (row_id, delta)&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt;  (1, 10),&lt;br /&gt;        (2, 20),&lt;br /&gt;        (3, 30),&lt;br /&gt;        (4, 40);&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;We can easily write a MERGE query to perform the actions needed.&amp;#160; Let’s also include an OUTPUT clause to show the effects on each row:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;MERGE   @Target T&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;USING&lt;/span&gt;   @Delta D&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;  D.row_id = T.row_id&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt;    MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;UPDATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; T.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; += D.delta&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt;    &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;BY&lt;/span&gt; TARGET &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;br /&gt;        INSERT &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; (D.row_id, D.delta)&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;OUTPUT&lt;/span&gt;  D.row_id,&lt;br /&gt;        $&lt;span style="color:#0000ff;"&gt;action&lt;/span&gt;,&lt;br /&gt;        DELETED.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; old_value,&lt;br /&gt;        INSERTED.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; new_value;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;If we examine the @Target table after executing this query, we get exactly the results shown in the diagram.&amp;#160; All good so far.&amp;#160; The problem comes when we look at the rows produced by the OUTPUT clause:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/MERGEOutput_4D9743E7.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="MERGE-Output" border="0" alt="MERGE-Output" src="http://sqlblog.com/blogs/paul_white/MERGEOutput_thumb_1AC73D73.png" width="234" height="97" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the action is ‘INSERT’, the &lt;i&gt;new_value&lt;/i&gt; column contains an incorrect value.&amp;#160; Instead of reporting the value actually inserted into the target table, it repeats the value associated with the last UPDATE operation.&amp;#160; If we change the data so that multiple INSERTs are performed after the UPDATE, all the INSERTs get the same &lt;i&gt;new_value&lt;/i&gt; associated with the prior UPDATE.&amp;#160; The @Target table always contains the correct data – it’s just that the OUTPUT clause disagrees.&lt;/p&gt;

&lt;h4&gt;The Query Plan&lt;/h4&gt;

&lt;p&gt;The original Connect item states that this issue only affects table variables, but there’s more to see here.&amp;#160; Let’s start by taking a look at the execution plan for the sample MERGE statement above (click to enlarge):&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/MergePlan_51A4226A.png"&gt;&lt;img style="border-right-width:0px;display:block;float:none;border-top-width:0px;border-bottom-width:0px;margin-left:auto;border-left-width:0px;margin-right:auto;" title="Merge-Plan" border="0" alt="Merge-Plan" src="http://sqlblog.com/blogs/paul_white/MergePlan_thumb_37CFEC3B.png" width="660" height="83" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have omitted several Compute Scalar iterators to make it easier to see what’s going on.&amp;#160; There’s a scan of the @Delta table, followed by an Insert to the @Target table, and finally a Merge into the @Target table.&amp;#160; If you are familiar with the sort of plans MERGE normally produces, you might find that arrangement a bit odd.&amp;#160; Even if MERGE is new to you, you might think that an Insert followed by a Merge is surprising.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;In a normal MERGE, we would expect to see an outer join between the source and target tables, followed by a Merge:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_0E5BAAFB.png"&gt;&lt;img style="border-right-width:0px;display:block;float:none;border-top-width:0px;border-bottom-width:0px;margin-left:auto;border-left-width:0px;margin-right:auto;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/paul_white/image_thumb_7E4F6CF4.png" width="660" height="154" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;SQL Server performs an outer join so the Clustered Index Merge knows whether the current row already exists in the target table or not.&amp;#160; If it exists, it is updated, otherwise a new row is inserted.&lt;/p&gt;

&lt;p&gt;If you’ve been following my &lt;a href="http://sqlblog.com/blogs/paul_white/archive/2010/07/29/inside-the-optimiser-constructing-a-plan.aspx"&gt;recent series&lt;/a&gt; on optimiser internals, you might suspect that the optimiser has applied a transformation to get the join-less plan, and the two plans are somehow logically identical.&amp;#160; You would be right – the rule in question is LOJPrjGetToApply (Left Outer Join, Project Get to Apply).&lt;/p&gt;

&lt;h4&gt;The Optimisation&lt;/h4&gt;

&lt;p&gt;The idea behind the optimisation is to read a row from the @Delta table and immediately try to insert it into the @Target table.&amp;#160; If that succeeds, the row didn’t already exist, and no further work is required.&lt;/p&gt;

&lt;p&gt;If the row did exist, the PRIMARY KEY on @Target will cause a duplicate key violation to occur.&amp;#160; The relational engine suppresses that error, and execution continues on down to the Clustered Index Merge iterator, which performs the necessary UPDATE.&lt;/p&gt;

&lt;p&gt;This is a very useful (and perfectly safe) query optimisation &lt;i&gt;if&lt;/i&gt; the conditions are right.&amp;#160; There are rather a lot of conditions, the most important of which are:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The target of the merge must have a suitable &lt;i&gt;clustered&lt;/i&gt; unique or primary key constraint &lt;/li&gt;

  &lt;li&gt;The changes (delta) table must have some sort of unique constraint (can be non-clustered) on the join column &lt;/li&gt;

  &lt;li&gt;The plan must be already using a nested loops join &lt;/li&gt;

  &lt;li&gt;The MERGE must include a NOT MATCHED condition plus at least one MATCHED clause &lt;/li&gt;

  &lt;li&gt;Equivalent columns in the two tables must have exactly the same type – no implicit conversions allowed &lt;/li&gt;

  &lt;li&gt;There can be no chance of a constraint violation – including NOT NULL constraints &lt;/li&gt;

  &lt;li&gt;The query must not update the clustered key &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If any of the required conditions do not apply, the optimisation is not performed, and the query plan retains the outer join.&lt;/p&gt;

&lt;p&gt;In order for the MERGE to return incorrect results:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The MERGE target must be a temporary table (the delta table can be anything) &lt;/li&gt;

  &lt;li&gt;The optimiser must produce a final plan that includes the transformation described above &lt;/li&gt;

  &lt;li&gt;The MERGE statement must include an OUTPUT clause that references the &lt;i&gt;inserted&lt;/i&gt; pseudo table &lt;/li&gt;

  &lt;li&gt;The query has to perform an INSERT row action after an UPDATE (in clustered index order) &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Importantly, it is only rows that result in an INSERT that cause problems.&amp;#160; If only UPDATE or DELETE operations result from the MERGE, everything works perfectly – even with a table variable as the target.&lt;/p&gt;

&lt;h4&gt;Producing an Access Violation&lt;/h4&gt;

&lt;p&gt;The example data given for the @Delta table was carefully constructed to ensure that incorrect results were returned.&amp;#160; With different sample data, we will get an Access Violation (AV) which terminates the connection and produces a stack dump on the SQL Server.&lt;/p&gt;

&lt;p&gt;The simplest way (based on the test rig above) is to change the @Delta table contents to just include a single row that will result in an INSERT:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;INSERT  @Delta&lt;br /&gt;        (row_id, delta)&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt;  (2, 2);&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;There is no row in @Target with &lt;i&gt;row_id&lt;/i&gt; = 2, so this results in an INSERT.&lt;/p&gt;

&lt;p&gt;To expand our test rig to encompass DELETE operations, we need to add an extra condition to the original MERGE statement.&amp;#160; The new code will delete a record if its value is zero after applying the deltas:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;MERGE   @Target T&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;USING&lt;/span&gt;   @Delta D&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;  D.row_id = T.row_id&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt;    MATCHED             &lt;span style="color:#008000;"&gt;-- New --&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;     T.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; = -D.delta  &lt;span style="color:#008000;"&gt;-- New --&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt;    &lt;span style="color:#0000ff;"&gt;DELETE&lt;/span&gt;              &lt;span style="color:#008000;"&gt;-- New --&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt;    MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;UPDATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; T.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; += D.delta&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt;    &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;BY&lt;/span&gt; TARGET &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;br /&gt;        INSERT &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; (D.row_id, D.delta)&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;OUTPUT&lt;/span&gt;  D.row_id,&lt;br /&gt;        $&lt;span style="color:#0000ff;"&gt;action&lt;/span&gt;,&lt;br /&gt;        DELETED.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; old_value,&lt;br /&gt;        INSERTED.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; new_value;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;With that modification in place, the following values in the @Delta table will update the row with &lt;i&gt;row_id&lt;/i&gt; = 1, and delete the one with &lt;i&gt;row_id&lt;/i&gt; = 3:&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;INSERT  @Delta&lt;br /&gt;        (row_id, delta)&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt;  (1, 10),&lt;br /&gt;        (3, -3);&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;This combination works correctly because there is no INSERT operation.&amp;#160; Any number of UPDATEs and DELETEs can be performed without issue, so long as no INSERTs occur.&lt;/p&gt;

&lt;h4&gt;Workarounds&lt;/h4&gt;

&lt;p&gt;The easiest workarounds involve preventing the query optimisation from happening in the first place.&amp;#160; This is quite easy since there are so many conditions for it to apply.&amp;#160; One way is to prevent the plan from using a nested loops join with an OPTION (HASH JOIN, MERGE JOIN) hint.&amp;#160; There are many other alternatives in the same vein, of course.&lt;/p&gt;

&lt;p&gt;A second workaround is to apply Trace Flag 8758 – unfortunately this disables a number of optimisations, not just the one above, so it’s not really recommended for long term use.&lt;/p&gt;

&lt;p&gt;Third, we could turn off the LOJPrjGetToApply rule (again, see my previous optimiser posts) but that could also negatively affect other plans that benefit from the optimisation.&lt;/p&gt;

&lt;h4&gt;Final Thoughts&lt;/h4&gt;

&lt;p&gt;This isn’t a bug that will affect everyone, but it does show that your chances of turning up a bug increase as you push the limits of the optimiser and fairly new features like MERGE.&lt;/p&gt;

&lt;p&gt;It will be interesting to see how the Connect item goes: will Microsoft fix this, or will they just say that MERGE was never intended for use with table variables?&amp;#160; We’ll have to wait and see.&lt;/p&gt;

&lt;p&gt;My thanks to ‘ALZDBA’ for reporting this problem.&amp;#160; Personally, I see the prospect of returning incorrect data from the OUTPUT clause as more serious than the access violation.&amp;#160; The OUTPUT clause is often used to record audit information, so producing incorrect data with no error seems undesirable.&amp;#160; I would encourage you all to form your own view on this bug, and &lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/581548/sql2008-r2-merge-statement-with-only-table-variables-fails"&gt;vote&lt;/a&gt; on it accordingly.&lt;/p&gt;

&lt;p&gt;© Paul White
  &lt;br /&gt;email: &lt;a href="mailto:SQLkiwi@gmail.com"&gt;SQLkiwi@gmail.com&lt;/a&gt;

  &lt;br /&gt;twitter: &lt;a href="http://twitter.com/SQL_Kiwi"&gt;@SQL_Kiwi&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>