SQL Merge Question

  • 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 :-)?

    DECLARE @BookInventory TABLE (TitleID INT, Title NVarchar(50), Quantity Int)

    DECLARE @BookOrder TABLE (TitleID INT, Title NVarchar(50), Quantity Int)

    DECLARE @BookChangeTracking TABLE (TitleID INT, OldQuantity Int, NewQuantity Int,ChangeDate DateTime)

    INSERT @BookInventory

    SELECT 1, 'The Catcher in the Rye', 6 UNION ALL

    SELECT 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 ALL

    SELECT 8, 'Slaughterhouse Five', 4;

    INSERT @BookOrder

    SELECT 1, 'The Catcher in the Rye', 3 UNION ALL

    SELECT 3, 'The Great Gatsby', 0 UNION ALL

    SELECT 4, 'Gone with the Wind', 4 UNION ALL

    SELECT 5, 'Jane Eyre', 5 UNION ALL

    SELECT 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

    DELETE

    WHEN MATCHED THEN

    INSERT INTO @BookChangeTracking(TitleID,OldQuantity,NewQuantity,ChangeDate)

    VALUES(bi.TitleID,bi.Quantity,bi.Quantity+bo.Quantity,getdate())

    UPDATE SET bi.Quantity = bi.Quantity + bo.Quantity

    WHEN NOT MATCHED BY TARGET THEN

    INSERT (TitleID, Title, Quantity)

    VALUES (bo.TitleID, bo.Title,bo.Quantity);

    select * from @BookInventory

  • 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.

  • I believe Jerry's answer is correct.

    What you want to do is this:

    WHEN MATCHED THEN BEGIN

    INSERT 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

    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.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply