T-SQL MERGE: My New Friend

As I was catching up on SQL 2008 this morning, I spent some time working with the T-SQL MERGE command, new in SQL 2008. Most of the blogs and examples online are a bit terse, so I decided to write a simple sample.

Check it out:

And here are the results:

Awesome! Admittedly, I’m still trying to come up with some legitimate uses for this in my day-to-day application programming 😀

You may also like...

3 Responses

  1. Mladen says:

    MERGE is especially useful for loading datawarehouses.
    one use i’ve found fo rin normal app developemnt is the
    if exists update
    else insert
    problem which merge solves beautifully.

  2. Chuck says:

    Yeah, that did pop up in my mind, but it seemed like it would be overkill since it would involve creating a table variable just to get it to work.

    I’ve usually handled this just by doing an EXISTS first and branching (INSERT or UPDATE) depending on if the object ID already exists.

  3. Mladen says:

    yeah i did the same thing, but it has it’s problems:
    http://weblogs.sqlteam.com/mladenp/archive/2007/07/30/60273.aspx
    and here’s the comparison for Merge:
    http://weblogs.sqlteam.com/mladenp/archive/2007/08/03/60277.aspx

    and to be complete a way to get total pessimistic concurrencything out of the way.
    Application Locks (or Mutexes) in SQL Server 2005:
    http://weblogs.sqlteam.com/mladenp/archive/2008/01/08/Application-Locks-or-Mutexes-in-SQL-Server-2005.aspx