﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / SQL Merge Question / 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>Sun, 19 May 2013 15:14:35 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: SQL Merge Question</title><link>http://www.sqlservercentral.com/Forums/Topic1379547-391-1.aspx</link><description>I believe Jerry's answer is correct.What you want to do is this:[code="sql"]WHEN MATCHED THEN BEGININSERT INTO @BookChangeTracking(TitleID,OldQuantity,NewQuantity,ChangeDate)VALUES(bi.TitleID,bi.Quantity,bi.Quantity+bo.Quantity,getdate())UPDATE SET bi.Quantity = bi.Quantity + bo.Quantity END[/code]But the MERGE statement doesn't support BEGIN like that.What you may be able to do is OUTPUT the results of the merge and use the $action variable available along with the INSERTED (pseudo-table) results to perform the INSERT into the @BookChangeTracking table.  Now that I think about it, this may not work because you can't put a WHERE clause on what gets inserted.  But you could INSERT all the results into a temp table and selectively insert into @BookChangeTracking from that in a separate statement (enclosed in a transaction of course).Another alternative may be to use "composable DML" to do the INSERT.  You should be able to Google the quoted term to read about it.  This may support the WHERE clause you'd need to make it work (not sure).Either way, I suggest you look to MS BOL for examples.Probably the best alternative (if you can get the conditions right) is to do the INSERT in a UPDATE trigger.</description><pubDate>Wed, 31 Oct 2012 23:46:14 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: SQL Merge Question</title><link>http://www.sqlservercentral.com/Forums/Topic1379547-391-1.aspx</link><description>This will most likely error out.  If I remember right, you can only have one action per MATCHED.  I think the INSERT statement will be interpretted as the last part of your MERGE statement.  This will result in an error on the next MATCHED.</description><pubDate>Wed, 31 Oct 2012 16:17:24 GMT</pubDate><dc:creator>jerry-621596</dc:creator></item><item><title>SQL Merge Question</title><link>http://www.sqlservercentral.com/Forums/Topic1379547-391-1.aspx</link><description>Experts.A sql merge question, from example below. Is there a way to make update @BookChangeTracking in the UPDATE condition like below (in bold)...can i do that kind of whacky stuffs or is it just too whacky to think about :-)?[code="sql"]DECLARE @BookInventory TABLE (TitleID INT, Title NVarchar(50), Quantity Int)DECLARE @BookOrder TABLE (TitleID INT, Title NVarchar(50), Quantity Int)[b]DECLARE @BookChangeTracking TABLE (TitleID INT, OldQuantity Int, NewQuantity Int,ChangeDate DateTime)[/b] INSERT @BookInventory SELECT 1, 'The Catcher in the Rye', 6 UNION ALLSELECT 2, 'Pride and Prejudice', 3 UNION ALL SELECT 3, 'The Great Gatsby', 0 UNION ALL SELECT 5, 'Jane Eyre', 0 UNION ALL SELECT 6, 'Catch 22', 0 UNION ALLSELECT 8, 'Slaughterhouse Five', 4;  INSERT @BookOrder SELECT 1, 'The Catcher in the Rye', 3 UNION ALLSELECT 3, 'The Great Gatsby', 0 UNION ALL SELECT 4, 'Gone with the Wind', 4  UNION ALLSELECT 5, 'Jane Eyre', 5 UNION ALLSELECT 7, 'Age of Innocence', 8; MERGE @BookInventory bi USING @BookOrder bo ON bi.TitleID = bo.TitleID  WHEN MATCHED AND  bi.Quantity + bo.Quantity = 0 THEN DELETEWHEN MATCHED THEN [b]INSERT INTO @BookChangeTracking(TitleID,OldQuantity,NewQuantity,ChangeDate)VALUES(bi.TitleID,bi.Quantity,bi.Quantity+bo.Quantity,getdate())[/b]UPDATE SET bi.Quantity = bi.Quantity + bo.Quantity WHEN NOT MATCHED BY TARGET THENINSERT (TitleID, Title, Quantity) VALUES (bo.TitleID, bo.Title,bo.Quantity);select * from @BookInventory[/code]</description><pubDate>Wed, 31 Oct 2012 15:19:14 GMT</pubDate><dc:creator>haiao2000</dc:creator></item></channel></rss>