The Power of Merge

  • Comments posted here are about the content posted at http://www.sqlservercentral.com/columnists/jSebastian/3122.asp

    .

  • But why use XML to pass data in, when SQL2008 gives you Table-Value Parameters?

    (I do love Merge - I spoke on it last month at the UG I run, and I'll be presenting on it in the next few months at other user-groups and at a code-camp too).

    Rob Farley
    LobsterPot Solutions & Adelaide SQL Server User Group
    Company: http://www.lobsterpot.com.au
    Blog: http://blogs.lobsterpot.com.au

  • Surely the set d.ItemNumber = o.ItemNumber isn't needed as the "join" is on OrderNumber and ItemNumber:

    MERGE OrderDetails AS d

       48 USING OrderInfo AS o

       49 ON (d.OrderNumber = o.OrderNumber AND d.ItemNumber = o.ItemNumber)

    WHEN MATCHED THEN

       51     UPDATE SET

       52         d.ItemNumber = o.ItemNumber,

     

    Regards

     

    Leo

  • Yes Leo, that's correct.

    Rob Farley
    LobsterPot Solutions & Adelaide SQL Server User Group
    Company: http://www.lobsterpot.com.au
    Blog: http://blogs.lobsterpot.com.au

  • Hi,

    Effectively, it seems really easy to use it. But what about performances? Is there any difference between using MERGE and using 'old' code ?

    And what about MERGE and global SQL specifications? Is there a chance to have this keyword appears on the next sql language norm?

    Anyway, thanks for this article. I'm impatient to install this new version to play with it

    Have a nice day

     

    PS: ALL WITH ME BEHIND FRENCH RUGBY TEAM (They need it :blush

  • Hi Rob,

    The focus of this article was to present the MERGE keyword and hence I started from where we stopped in a previous article.

    There is another article scheduled, which introduces the table-value parameter and will be out in the next few days.

    thanks

    Jacob

    .

  • Agreed with you, Leo.

    .

  • Im familiar with the scenario, but Im not sure I'm sold yet that this is substantially better - one more syntax with quirks you have to learn! Performance could be a key point, I look forward to seeing how it performs.

  • Merge has been present in DB2 for years, it is a good thing that MS is catching up. Personally I find it handy for cetain things but I am afraid that this can be used on situations where a better application design is the key.

    Thanks for the article. Simple and to the point.


    * Noel

  • What about REPLACE INTO?  That would be easier on the coders.  The way it's supposed to work is that if the primary key does not exists the row gets inserted.  If the row does exist it gets replaced.  There was some talk that rows affected would come ack as 1 if the insert happened and 2 if the row was replaced.  The thinking being that the replace was like a delete and an insert.

    If I'm going to have to code procedures then I'll just code the procedures.  There is only one possible benefit from doing things the MERGE way.  I can update an existing order in my order table but new orders could get put into a new_order table.  Why you would want to do that I don't know.

    I was jumping up and down happy when I read the article headline and then so dissapointed when I saw the syntax.

    ATBCharles Kincaid

  • Great example, and thanks for the info on the MERGE functionality.  A question about usage...

    With this syntax:

    WHEN SOURCE NOT MATCHED THEN

            DELETE

    Can I put other things in there instead of "DELETE"?  I don't physically delete from my databases - we use a logical delete using an InactiveDate field.  Is there a way to set that InactiveDate field for those records that are in the source but not matched with any data in the XML (and therefore need to be "deleted")?

    Thanks.

  • What triggers get fired with Merge ? If it's more then one trigger, is there a fixed order or can they be fired in a random order ?

    Thanks,

    Don

  • Here is an example:

    WHEN

    SOURCE NOT MATCHED THEN

        --DELETE

        UPDATE SET

        d

    .Deleted = 1

    The above code updates the status of the column "deleted" to 1, instead of physically deleting the record.

    If you are doing this,  you need another change at the JOIN condition, to skip the deleted records.

    USING

    OrderInfo AS o

    ON

    (d.OrderNumber = o.OrderNumber AND d.ItemNumber = o.ItemNumber AND d.Deleted = 0)

     

    .

  • Cool.  Then how do you undeleted a deleted record?

    ATBCharles Kincaid

  • From my testing, i found that the triggers are always fired in the following order.

    1. insert

    2. update

    3. delete

    I am not sure if we can always assume this order.

     

    .

Viewing 15 posts - 1 through 15 (of 27 total)

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