﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Hugo Kornelis  / MERGE with ANSI / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 02:34:00 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>My brain almost exploded..lol!! Pretty hard question. I actually have to open my book and read more about MERGE statement.  I was under impression that this MERGE functionality came in SQL2008 only since I start using it in SQL2008 only.  I didn't get lots of opportunities to play with newer functionality in 2005.One thing I would like to point out is that MERGE statement does allows BY SOURCE and BY TARGET in the statement.  Here's the snippet of code where I use both:MERGE dbo.column_collection AS ccUSING dbo.staging_column_collection AS sccON (      scc.table_name = cc.table_name      AND scc.database_name = cc.database_name      AND scc.ordinal_position = cc.ordinal_position      AND scc.record_status = cc.record_status      AND scc.schema_name = cc.schema_name      AND cc.record_status = 'A'      AND scc.platform_type_code = cc.platform_type_code      AND scc.database_name NOT LIKE '_broken%'      )WHEN MATCHED AND cc.physical_column_name &amp;lt;&amp;gt; scc.physical_column_name                   OR cc.data_type &amp;lt;&amp;gt; scc.data_type                   OR cc.column_length &amp;lt;&amp;gt; scc.column_lengthTHEN UPDATE           SET cc.physical_column_name = scc.physical_column_name            , cc.column_name = scc.column_name            , cc.data_type = scc.data_type            , cc.column_length = scc.column_length              WHEN NOT MATCHED BY TARGET THEN      INSERT             (               column_name             , physical_column_name             , data_type             , column_length             , column_definition             , record_status, insert_date             , table_name, database_name             , ordinal_position             , schema_name             , platform_type_code             )            VALUES                   (                    scc.column_name                  , scc.physical_column_name                  , scc.data_type                  , scc.column_length                  , scc.column_definition                  , scc.record_status                  , scc.insert_date                  , scc.table_name                  , scc.database_name                  , scc.ordinal_position                  , schema_name                  , platform_type_code                  )WHEN NOT MATCHED BY SOURCE THEN UPDATE             SET cc.record_status = 'D'</description><pubDate>Thu, 29 Dec 2011 15:15:56 GMT</pubDate><dc:creator>sbamaty</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>Challenging question. Thanks for submitting.</description><pubDate>Tue, 29 Nov 2011 06:17:18 GMT</pubDate><dc:creator>Britt Cluff</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>thanks Hugo!</description><pubDate>Mon, 28 Nov 2011 16:10:28 GMT</pubDate><dc:creator>OzYbOi d(-_-)b</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>Thanks Hugo</description><pubDate>Mon, 28 Nov 2011 09:26:12 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>I have unbfair advantage: MERGE is my daily bread.  ;-)Thanks for this interesting question!</description><pubDate>Mon, 28 Nov 2011 09:18:22 GMT</pubDate><dc:creator>Revenant</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>A very tough question....</description><pubDate>Mon, 28 Nov 2011 00:08:34 GMT</pubDate><dc:creator>Anipaul</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>Tough one, thanks.</description><pubDate>Sun, 27 Nov 2011 23:27:52 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>Nice question Hugo, had to do some research before answering this one and I still was surprised that I got it right. I think my brain now hurts too much to get anything else done today. Happy Friday!</description><pubDate>Fri, 25 Nov 2011 11:57:37 GMT</pubDate><dc:creator>KWymore</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>[quote][b]martin.whitton (11/25/2011)[/b][hr]The MERGE statement is only available in SQL Server 2008 and later.I don't think this has any bearing on the low percentage of people getting this right, though...![/quote]Actually the percentage doesn't seem too bad - up to now 12% got it right; that's a bit more than four times as many as would be expected if everyone ticked 4 boxes chosen at random from the seven boxes provided.  There have been much worse responses to other questions.But I agree this one was pretty hard.</description><pubDate>Fri, 25 Nov 2011 09:20:35 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>Good question and good explanation - and thanks especially for the link to a late draft of the SQL 2003 standard.It was a bit hard for a Friday question, just when people are getting ready to relax for the weekend (careless scheduling by Steve there, or did he do it on purpose?).  I spent a long time racking my brain to try to remember which was the fourth box to tick - after I had ticked three boxes all the others seemed as if they ought to be left unticked; and then I clicked that only UPDATE was allowed in WHEN MATCHED, not DELETE, so as I had already ticked WHEN NOT MATCHED BY SOURCE I logically had to tick DELETE as my fourth choice.  Definitely good excercise for the grey matter.</description><pubDate>Fri, 25 Nov 2011 09:11:28 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>Thanks, all, for the kind words! I am glad you all find my questions entertaining and informative.[quote][b]martin.whitton (11/25/2011)[/b][hr]The MERGE statement is only available in SQL Server 2008 and later.[/quote]Oops! :blush: Stupid mistake - sorry 'bout that![quote][b]Stewart "Arturius" Campbell (11/25/2011)[/b][hr]I think the main reason for the poor score rate is that the SQL ANSI:2003 definitions are not readily available.[/quote]I included a link to a freely available version (of a late draft, not the official final version) in the answer's explanation. When I first found this link, I immediately downloaded it to my computer for fear of it being removed. When I submitted the question, I had to use Google to re-find the link.As far as I know, there are no newer versions of the ANSI SQL standard that are freely available (unfortunately).By the way - after submitting this question, I found that most of the non-SQL:2003 stuff in MERGE was not added by Microsoft for SQL Server only, but is defined in SQL:2008. So I was very glad that I included the version of the standard in the question! :-D</description><pubDate>Fri, 25 Nov 2011 03:20:08 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>[quote]I came across this interesting and helpful [url=http://sqlblog.com/blogs/hugo_kornelis/archive/2008/03/10/lets-deprecate-update-from.aspx]blog post[/url] by one [i]Hugo Kornelis[/i] :-).Hence a double thank you to Hugo for an excellent QotD on MERGE following the ANSI standard, and for sharing your extensive knowledge with everybody on the web.l[/quote]Thank you Michael (and Hugo!) for that very useful link.Despite my minor bit of pedantry about the SQL versions, I second your thanks to Hugo - I always read his contributions with interest and often learn from them. :-)</description><pubDate>Fri, 25 Nov 2011 02:41:53 GMT</pubDate><dc:creator>martin.whitton</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>My brain hurts...</description><pubDate>Fri, 25 Nov 2011 02:37:20 GMT</pubDate><dc:creator>adb2303</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>[quote][b]Stewart "Arturius" Campbell (11/25/2011)[/b][hr]This was quite a tough one.Learned a fair bit about the origin of the MERGE statement from this.[/quote]I definitely second this.Some may argue it's cheating to do some reading (researching) prior to answering the QotD, but I came across this interesting and helpful [url=http://sqlblog.com/blogs/hugo_kornelis/archive/2008/03/10/lets-deprecate-update-from.aspx]blog post[/url] by one [i]Hugo Kornelis[/i] :-).Hence a double thank you to Hugo for an excellent QotD on MERGE following the ANSI standard, and for sharing your extensive knowledge with everybody on the web.You all have a great weekend,Michael</description><pubDate>Fri, 25 Nov 2011 02:20:43 GMT</pubDate><dc:creator>michael.kaufmann</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>This was quite a tough one.Learned a fair bit about the origin of the MERGE statement from this.Took a fair amount of thinking and digging to get it, though.I think the main reason for the poor score rate is that the SQL ANSI:2003 definitions are not readily available.</description><pubDate>Fri, 25 Nov 2011 01:54:41 GMT</pubDate><dc:creator>Stewart "Arturius" Campbell</dc:creator></item><item><title>RE: MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>[quote]Given the MERGE statement below, which is completely correct for SQL Server 2005 and later[/quote]The MERGE statement is only available in SQL Server 2008 and later.I don't think this has any bearing on the low percentage of people getting this right, though...!</description><pubDate>Fri, 25 Nov 2011 01:39:40 GMT</pubDate><dc:creator>martin.whitton</dc:creator></item><item><title>MERGE with ANSI</title><link>http://www.sqlservercentral.com/Forums/Topic1211805-1328-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/76898/"&gt;MERGE with ANSI&lt;/A&gt;[/B]</description><pubDate>Thu, 24 Nov 2011 23:31:04 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item></channel></rss>