<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: MERGE gives better OUTPUT options	</title>
	<atom:link href="https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/feed/" rel="self" type="application/rss+xml" />
	<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/</link>
	<description></description>
	<lastBuildDate>Tue, 25 Oct 2016 19:57:01 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>
		By: Karl Ninh		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2117</link>

		<dc:creator><![CDATA[Karl Ninh]]></dc:creator>
		<pubDate>Tue, 25 Oct 2016 19:57:01 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2117</guid>

					<description><![CDATA[Can you output to a file?]]></description>
			<content:encoded><![CDATA[<p>Can you output to a file?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ritesh Patel		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2116</link>

		<dc:creator><![CDATA[Ritesh Patel]]></dc:creator>
		<pubDate>Wed, 05 Oct 2016 13:21:39 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2116</guid>

					<description><![CDATA[Hi Rob,
The audit table part of your query does not quiet work as expected. Although it is capturing the changes into #someaudittable it is still also performing the merge action on the #tmp table.
Is there any way to prevent the actual merge on #tmp and just getting the &#039;proposed&#039; changes into #someaudittable?
Thanks,
Ritesh]]></description>
			<content:encoded><![CDATA[<p>Hi Rob,<br />
The audit table part of your query does not quiet work as expected. Although it is capturing the changes into #someaudittable it is still also performing the merge action on the #tmp table.<br />
Is there any way to prevent the actual merge on #tmp and just getting the &#8216;proposed&#8217; changes into #someaudittable?<br />
Thanks,<br />
Ritesh</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2115</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Wed, 11 Mar 2015 01:01:33 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2115</guid>

					<description><![CDATA[Hi Pete - yes. :)]]></description>
			<content:encoded><![CDATA[<p>Hi Pete &#8211; yes. 🙂</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Peter Nightingale		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2114</link>

		<dc:creator><![CDATA[Peter Nightingale]]></dc:creator>
		<pubDate>Tue, 10 Mar 2015 18:53:54 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2114</guid>

					<description><![CDATA[Correction, the IDENTITY column does appear in the INSERTED table, but not in the target table.
So v useful actually. (ahem)]]></description>
			<content:encoded><![CDATA[<p>Correction, the IDENTITY column does appear in the INSERTED table, but not in the target table.<br />
So v useful actually. (ahem)</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Pete Nightingale		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2113</link>

		<dc:creator><![CDATA[Pete Nightingale]]></dc:creator>
		<pubDate>Tue, 10 Mar 2015 18:48:13 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2113</guid>

					<description><![CDATA[Interesting but this method does not actually allow you to pickup the values applied with an IDENTITY column as they so not appear in the INSERTED table.
Suppose I had better write a loop then, good job I am covering a rare event.]]></description>
			<content:encoded><![CDATA[<p>Interesting but this method does not actually allow you to pickup the values applied with an IDENTITY column as they so not appear in the INSERTED table.<br />
Suppose I had better write a loop then, good job I am covering a rare event.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2112</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Wed, 14 Jan 2015 11:36:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2112</guid>

					<description><![CDATA[Sorry Dipz, you don&#039;t have access to that information in an INSERT statement.]]></description>
			<content:encoded><![CDATA[<p>Sorry Dipz, you don&#8217;t have access to that information in an INSERT statement.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dipankar Saha		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2111</link>

		<dc:creator><![CDATA[Dipankar Saha]]></dc:creator>
		<pubDate>Wed, 14 Jan 2015 11:09:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2111</guid>

					<description><![CDATA[Hi Rob,
I was working on a requirement where i need to access inserted.* values and values from the Select List in the output clause for an insert statement. I am working in 2005 so i cannot use merge statement which i know is the other way around but wanted to clear my doubt that its techinically not possible or i am missing something.
PFB, the code part.
 &#160; &#160; &#160; INSERT INTO ExternalEmployeeNumber(ExternalEmployeeNumber,DateUsed) 
 &#160; &#160; &#160; &#160;SELECT &#160;dateadd(second, ROWVAL, DateVale)
 &#160; &#160; &#160; &#160;FROM
 &#160; &#160; &#160; &#160;( &#160; SELECT B.[FIRST NAME] FNAME, B.[LAST NAME] LNAME , B.EMAIL EMAIL , GETDATE() DateVale , ROW_NUMBER ( ) 
			OVER (ORDER BY B.[FIRST NAME], B.[LAST NAME] , B.EMAIL) ROWVAL
			FROM dbo.[EOP_REMs$] B where B.[last name] is not null and B.[first name] is not null and B.[email] is not null
			and NOT EXISTS ( SELECT 1 FROM employee E WHERE B.[first name] = E.[firstname] &#160;AND B.[email] = E.[email] AND B.[last name] = E.[lastname] )
 &#160; &#160; &#160; &#160;) X
 &#160; &#160; &#160; &#160;OUTPUT INSERTED.ExternalEmployeeNumber , FNAME , LNAME , EMAIL
 &#160; &#160; &#160; &#160; &#160; &#160; &#160;INTO #EmployeeNew([Employee#],[FirstName],[LastName],[Email]); 
In the code part above, i am using Fname, Lname and Email other than the inserted column. 
It is giving me an error , invalid reference of the Fname, Lname, Email column name ?
Can you help me out. As i said i implemented this using Merge and its working but i need to implement this in SQL 2005.
Thanks and Regards
Dipz]]></description>
			<content:encoded><![CDATA[<p>Hi Rob,<br />
I was working on a requirement where i need to access inserted.* values and values from the Select List in the output clause for an insert statement. I am working in 2005 so i cannot use merge statement which i know is the other way around but wanted to clear my doubt that its techinically not possible or i am missing something.<br />
PFB, the code part.<br />
 &nbsp; &nbsp; &nbsp; INSERT INTO ExternalEmployeeNumber(ExternalEmployeeNumber,DateUsed)<br />
 &nbsp; &nbsp; &nbsp; &nbsp;SELECT &nbsp;dateadd(second, ROWVAL, DateVale)<br />
 &nbsp; &nbsp; &nbsp; &nbsp;FROM<br />
 &nbsp; &nbsp; &nbsp; &nbsp;( &nbsp; SELECT B.[FIRST NAME] FNAME, B.[LAST NAME] LNAME , B.EMAIL EMAIL , GETDATE() DateVale , ROW_NUMBER ( )<br />
			OVER (ORDER BY B.[FIRST NAME], B.[LAST NAME] , B.EMAIL) ROWVAL<br />
			FROM dbo.[EOP_REMs$] B where B.[last name] is not null and B.[first name] is not null and B.[email] is not null<br />
			and NOT EXISTS ( SELECT 1 FROM employee E WHERE B.[first name] = E.[firstname] &nbsp;AND B.[email] = E.[email] AND B.[last name] = E.[lastname] )<br />
 &nbsp; &nbsp; &nbsp; &nbsp;) X<br />
 &nbsp; &nbsp; &nbsp; &nbsp;OUTPUT INSERTED.ExternalEmployeeNumber , FNAME , LNAME , EMAIL<br />
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;INTO #EmployeeNew([Employee#],[FirstName],[LastName],[Email]);<br />
In the code part above, i am using Fname, Lname and Email other than the inserted column.<br />
It is giving me an error , invalid reference of the Fname, Lname, Email column name ?<br />
Can you help me out. As i said i implemented this using Merge and its working but i need to implement this in SQL 2005.<br />
Thanks and Regards<br />
Dipz</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sean Peffer		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2110</link>

		<dc:creator><![CDATA[Sean Peffer]]></dc:creator>
		<pubDate>Fri, 03 Oct 2014 12:22:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2110</guid>

					<description><![CDATA[Thanks for this great explaantion of the OUTPUT/$ACTION - very useful.
I used the MERGE statement to update a SCD2 Dimension and it&#039;s great that it can be done in a single step iso multiple steps and temp tables. I now require to capture the Audit info as well and this function helps with this requirement as well. Here&#039;s an exmaple used inside a stored proc to return the audit info
DECLARE @Results TABLE(Action VARCHAR(20));
/.......Merge statements here...../
OUTPUT $action INTO @Results;
	SELECT	@Insert=[INSERT],
		@Update=[UPDATE],
		@Delete=[DELETE]
	FROM &#160; &#160;(SELECT	Action,
			 &#160; &#160; 1 ROWS
			FROM &#160;@Results) P
			PIVOT (COUNT(ROWS)
			FOR &#160;Action IN &#160;([INSERT], [UPDATE], [DELETE])) AS pvt;]]></description>
			<content:encoded><![CDATA[<p>Thanks for this great explaantion of the OUTPUT/$ACTION &#8211; very useful.<br />
I used the MERGE statement to update a SCD2 Dimension and it&#8217;s great that it can be done in a single step iso multiple steps and temp tables. I now require to capture the Audit info as well and this function helps with this requirement as well. Here&#8217;s an exmaple used inside a stored proc to return the audit info<br />
DECLARE @Results TABLE(Action VARCHAR(20));<br />
/&#8230;&#8230;.Merge statements here&#8230;../<br />
OUTPUT $action INTO @Results;<br />
	SELECT	@Insert=[INSERT],<br />
		@Update=[UPDATE],<br />
		@Delete=[DELETE]<br />
	FROM &nbsp; &nbsp;(SELECT	Action,<br />
			 &nbsp; &nbsp; 1 ROWS<br />
			FROM &nbsp;@Results) P<br />
			PIVOT (COUNT(ROWS)<br />
			FOR &nbsp;Action IN &nbsp;([INSERT], [UPDATE], [DELETE])) AS pvt;</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2109</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Fri, 26 Sep 2014 08:59:13 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2109</guid>

					<description><![CDATA[Thanks Mark - that&#039;s an interesting idea. :)]]></description>
			<content:encoded><![CDATA[<p>Thanks Mark &#8211; that&#8217;s an interesting idea. 🙂</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Mark Kennedy		</title>
		<link>https://lobsterpot.com.au/blog/2012/06/12/merge-gives-better-output-options/#comment-2108</link>

		<dc:creator><![CDATA[Mark Kennedy]]></dc:creator>
		<pubDate>Thu, 18 Sep 2014 12:59:35 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3122#comment-2108</guid>

					<description><![CDATA[You can access data other than the inserted/deleted in an output clause, but it has to be joined.
There&#039;s examples on the OUTPUT clause MSDN page, example E is where I got the idea to try self join.
&lt;a href=&quot;http://msdn.microsoft.com/en-gb/library/ms177564&quot; rel=&quot;nofollow ugc&quot;&gt;http://msdn.microsoft.com/en-gb/library/ms177564&lt;/a&gt;(v=sql.105).aspx
but try this:
UPDATE #tmp
SET someval += 1
OUTPUT deleted.*, inserted.someval, t.id
FROM #tmp
INNER JOIN #tmp as t ON #tmp.id = t.id
I don&#039;t know why exactly, but its probably something to do with not being able to use values on the table directly being changed, but joining to its self seems to work. 
I did this on SQL server 2008R2
Saves having to delve into using merge statements if you just want to get other data in an OUTPUT from a simple update.]]></description>
			<content:encoded><![CDATA[<p>You can access data other than the inserted/deleted in an output clause, but it has to be joined.<br />
There&#8217;s examples on the OUTPUT clause MSDN page, example E is where I got the idea to try self join.<br />
<a href="http://msdn.microsoft.com/en-gb/library/ms177564" rel="nofollow ugc">http://msdn.microsoft.com/en-gb/library/ms177564</a>(v=sql.105).aspx<br />
but try this:<br />
UPDATE #tmp<br />
SET someval += 1<br />
OUTPUT deleted.*, inserted.someval, t.id<br />
FROM #tmp<br />
INNER JOIN #tmp as t ON #tmp.id = t.id<br />
I don&#8217;t know why exactly, but its probably something to do with not being able to use values on the table directly being changed, but joining to its self seems to work.<br />
I did this on SQL server 2008R2<br />
Saves having to delve into using merge statements if you just want to get other data in an OUTPUT from a simple update.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
