SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Question to you all on MERGE


Question to you all on MERGE

Author
Message
sqlPirate_Fl
sqlPirate_Fl
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 77
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.
Orlando Colamatteo
Orlando Colamatteo
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40364 Visits: 14413
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
Steven Willis
Steven Willis
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2075 Visits: 1721
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;



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search