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

<channel>
	<title>MERGE &#8211; SQLpowered.com</title>
	<atom:link href="https://sqlpowered.com/tag/merge/feed/" rel="self" type="application/rss+xml" />
	<link>https://sqlpowered.com</link>
	<description>SQL Server + BI</description>
	<lastBuildDate>Tue, 06 Apr 2021 19:05:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://sqlpowered.com/wp-content/uploads/2020/07/FavIcon-e1594067873682-99x100.png</url>
	<title>MERGE &#8211; SQLpowered.com</title>
	<link>https://sqlpowered.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Using MERGE to override OUTPUT clause limitations</title>
		<link>https://sqlpowered.com/using-merge-to-override-output-clause-limitations/</link>
					<comments>https://sqlpowered.com/using-merge-to-override-output-clause-limitations/#respond</comments>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Tue, 07 Jul 2020 21:46:39 +0000</pubDate>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[MERGE]]></category>
		<category><![CDATA[SELECT]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?p=4148</guid>

					<description><![CDATA[The OUTPUT clause is a very powerful extension of T-SQL included in SQL Server 2005. But from time to time it needs some tweaks or workarounds to get the expected result. One such example is using OUTPUT with INSERT statements. There is a limitation that only columns from the table...]]></description>
										<content:encoded><![CDATA[<p>The OUTPUT clause is a very powerful extension of T-SQL included in SQL Server 2005. But from time to time it needs some tweaks or workarounds to get the expected result. One such example is using OUTPUT with INSERT statements. There is a limitation that only columns from the table to which we are inserting can be used in OUTPUT. I will show you how to override this limitation with a small trick using the MERGE statement added to SQL Server in version 2008.</p>
<p>Let&#8217;s prepare two tables we will play with. One of them will be [dbo].[TargetTable] where data will be inserted from the second table [dbo].[SourceTable].</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">DROP TABLE IF EXISTS [dbo].[TargetTable]
DROP TABLE IF EXISTS [dbo].[SourceTable]
GO

CREATE TABLE [dbo].[TargetTable] (
	[ID] INT NOT NULL,
	[Value] NVARCHAR(100)
)
GO

CREATE TABLE [dbo].[SourceTable] (
	[ID] INT NOT NULL,
	[Value] NVARCHAR(100),
	[OtherValue] NVARCHAR(100)
)
GO

INSERT INTO [dbo].[SourceTable]
	( [ID], [Value], [OtherValue] )
	VALUES 
	(1, 'Val1', 'OthVal1' ),
	(2, 'Val2', 'OthVal2' ),
	(3, 'Val3', 'OthVal3' )
GO</pre>
<p>Once everything is prepared we can try the following statement to insert data from the source table to the target table. We would like also to output some columns we will use for review or processing later (if we will use table or table variable as OUTPUT target):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">INSERT INTO [dbo].[TargetTable]
	( [ID],	[Value] )
		OUTPUT [Inserted].[ID], [Inserted].[Value], [src].[OtherValue]
	SELECT 
		[src].[ID], [src].[Value]
	FROM [dbo].[SourceTable] [src]
GO</pre>
<p><img decoding="async" class="alignnone wp-image-4408" src="https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-1.png" alt="" width="357" height="25" srcset="https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-1.png 672w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-1-300x21.png 300w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-1-150x10.png 150w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-1-360x25.png 360w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-1-160x11.png 160w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-1-320x22.png 320w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-1-520x36.png 520w" sizes="(max-width: 357px) 100vw, 357px" /></p>
<p>The statement has failed because of the column [src].[OtherValue] isn&#8217;t one of the columns inserted into the target table and therefore it isn&#8217;t allowed to be referenced in the OUTPUT clause.</p>
<p>But what should be done if we really need this column in the output? We can simply get it when MERGE will be used instead of INSERT:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">MERGE INTO [dbo].[TargetTable] [trg]
USING ( SELECT [ID], [Value], [OtherValue]
		FROM [dbo].[SourceTable]
	  ) AS [src] ON 1 = 0
WHEN NOT MATCHED THEN
	INSERT ( [ID],[Value] )
	VALUES ([src].[ID], [src].[Value])
		OUTPUT [Inserted].[ID], [Inserted].[Value], [src].[OtherValue];
GO</pre>
<p><img decoding="async" class="alignnone wp-image-4409" src="https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-2.png" alt="" width="173" height="67" srcset="https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-2.png 392w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-2-300x116.png 300w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-2-150x58.png 150w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-2-360x140.png 360w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-2-160x62.png 160w, https://sqlpowered.com/wp-content/uploads/2020/07/OUTPUT-Clause-With-Outer-Column-Reference-2-320x124.png 320w" sizes="(max-width: 173px) 100vw, 173px" /></p>
<p>MERGE is much flexible to be used together with the OUTPUT clause and is suitable for a lot of special scenarios like this one. INSERT still have its place. Using MERGE everywhere instead of INSERTED isn&#8217;t a good idea. People reading our code will be confused and MERGE has still a lot of <a href="https://www.mssqltips.com/sqlservertip/3074/use-caution-with-sql-servers-merge-statement/">issues and hidden drawbacks</a> unresolved.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlpowered.com/using-merge-to-override-output-clause-limitations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
