Question to you all on MERGE

  • So I've been reading up on Merge and have been experimenting with it lately in my 2012 environment.

    I've run a few tests and was curious if aside from the readability improvements offered, if you found it useful in your production environment. Opinions welcome.

    Also for your source tables, I've only tested against @temp tables so far, but did you have better performance with #tables or physical tables? In the latter two, assuming proper indexing added.

    Thanks for reading.

    P.S. I find MERGE incredibly strong, but dangerous during a delete if you're clause isn't accurate. I like it personally, but before I start using it in real life situations just looking for the opinions of others who've used it successfully in their day to day work environment.

  • sqlPirate_Fl (3/7/2013)


    So I've been reading up on Merge and have been experimenting with it lately in my 2012 environment.

    I've run a few tests and was curious if aside from the readability improvements offered, if you found it useful in your production environment. Opinions welcome.

    Also for your source tables, I've only tested against @temp tables so far, but did you have better performance with #tables or physical tables? In the latter two, assuming proper indexing added.

    Thanks for reading.

    I use MERGE often. In addition to the scenario where multiple operations are useful, I also use it in place of single UPDATE...FROM/JOIN operations because it protects us from ambiguously updating the same row multiple times due to an improper JOIN clause.

    P.S. I find MERGE incredibly strong, but dangerous during a delete if you're clause isn't accurate. I like it personally, but before I start using it in real life situations just looking for the opinions of others who've used it successfully in their day to day work environment.

    I would say that a MERGE that deletes data is no more dangerous than a DELETE...FROM/JOIN that deletes data. The risk in writing an improper JOIN clause seems to be about the same. Maybe not initially though, but once you learn and are comfortable with the MERGE syntax.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • I always use MERGE now rather than several IF blocks (unless I'm stuck with SQL2005 and earlier). It's important to test the merge before using it on live data though! I have a template I use with a hot-key to drop the basic structure into a new procedure. If you are new to MERGE you may not know that the DELETE/INSERT/UPDATE statements can be in different orders and each statement will accept any additional logic you want to apply.

    You can do all sorts of things like:

    ;MERGE INTO dbo.[TargetTable] AS [Target]

    USING (SELECT ID AS TargetTableID FROM dbo.[BookTable]) AS [Source]

    ON [Target].TargetTableID = [Source].TargetTableID

    WHEN MATCHED AND [Target].isDelete = 1

    THEN DELETE

    WHEN MATCHED AND NULLIF([Source].Title,'') IS NOT NULL

    THEN UPDATE

    SET

    [Target].Title = [Source].Title

    ,[Target].Text = [Source].Text

    WHEN NOT MATCHED BY TARGET AND NULLIF([Source].Title,'') IS NOT NULL

    THEN INSERT

    (

    Title

    ,Text

    )

    VALUES

    (

    [Source].Title

    ,[Source].Text

    )

    WHEN NOT MATCHED BY SOURCE AND YEAR([Target].DatePublished) < 2013

    THEN DELETE;

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

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