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


The Power of Merge


The Power of Merge

Author
Message
Jason1972
Jason1972
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 167

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.


Donald Desnoyer
Donald Desnoyer
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 34
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
jacob sebastian
jacob sebastian
SSC Eights!
SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)

Group: General Forum Members
Points: 998 Visits: 2523

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)



.
Charles Kincaid
Charles Kincaid
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1623 Visits: 2384
Cool. Then how do you undeleted a deleted record?

ATBCharles Kincaid
jacob sebastian
jacob sebastian
SSC Eights!
SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)

Group: General Forum Members
Points: 998 Visits: 2523

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.



.
jacob sebastian
jacob sebastian
SSC Eights!
SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)SSC Eights! (998 reputation)

Group: General Forum Members
Points: 998 Visits: 2523

Just tried this and it works:

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

WHEN MATCHED THEN

UPDATE SET

d.Qty = o.Qty,

d.Rate = o.Rate,

d.Deleted = 0

WHEN NOT MATCHED THEN

INSERT (OrderNumber, ItemNumber, Qty, Rate)

VALUES (o.OrderNumber, o.ItemNumber, o.Qty, o.Rate)

WHEN SOURCE NOT MATCHED THEN

UPDATE SET d.Deleted = 1



.
Wayne West
Wayne West
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2582 Visits: 3702
Merge is part of the ISO SQL standard, the MS implementation always seems to lag a generation or so behind ISO.

There's nothing saying we have to use it, like so many other things it's just a tool that's available to us. It will be interesting to see some performance benchmarks, even if they're less than formal.

-----
Knowledge is of two kinds. We know a subject ourselves or we know where we can find information upon it. --Samuel Johnson
J.Wilson
J.Wilson
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 13

Hi Jacob

What version sqlserver 2005 I must use. Why version .3042 it generates syntax error...with the commando MERGE.

thank you.


Wayne West
Wayne West
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2582 Visits: 3702
It's in the 2008 beta, not in 2005.

-----
Knowledge is of two kinds. We know a subject ourselves or we know where we can find information upon it. --Samuel Johnson
Ian Yates
Ian Yates
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1798 Visits: 445

Much desired new feature - should make the CRUD procedures a bit easier

I like the idea floated about flagging records as deleted rather than actually deleting them. I haven't experimented with 2008 yet (and a few of our customers are still running 2000 so I cannot even take advantage of this for a while!) but I think there will be many creative uses for the command as you needn't necessarily insert a record when not matched, just as you needn't necessarily delete.

Presumably only a single command can be executed - no begin/end blocks? Also, I would assume that the command has to be set based rather than, for example, a stored procedure execution (which would make the command cursor-based and ugly).

I would think the merge command allows the query optimiser to have some smarts such as avoiding multiple table/index scans that may occur if you do the
IF EXISTS... UPDATE... ELSE INSERT... (although the optimiser may already recognise this - I haven't checked lately).





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