• 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