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


Importance of UPSERT


Importance of UPSERT

Author
Message
Roy Ernest
Roy Ernest
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8403 Visits: 6891
Thanks Vincy. In SQL 2008 there is a command called Merge that does almost the same thing that I discussed. This was written during the SQL 2000/2005 world.

-Roy
Charles Kincaid
Charles Kincaid
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3217 Visits: 2384
Have we talked about REPLACE INTO? I'm still longing for the alternative syntax for INSERT.



INSERT INTO [###_120978_@@#@]
SET [ Key]='2863485278',
[ CustomerId]=78496,
[ ShippingAddressID]=12782,
[ TakenByID]=345,
[ OrderStarted]=GETUTCDATE()




ATBCharles Kincaid
Roy Ernest
Roy Ernest
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8403 Visits: 6891
That peace of code looks like a cross between Insert and an Update... :-)

-Roy
Charles Kincaid
Charles Kincaid
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3217 Visits: 2384
Roy Ernest (5/11/2010)
That peace of code looks like a cross between Insert and an Update... :-)
Exactly right. Now notice that all I would have to do is replace INSERT INTO with UPDATE and add a WHERE clause. The place REPLACE INTO where it is implemented rows affected is 1 if it did an insert but 2 (or more) if it did an update.

ATBCharles Kincaid
paul s-306273
paul s-306273
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3466 Visits: 1169
The article states that it's more efficient to do an 'update' first. I'd have thought this is true if you have more updates than inserts. If you actually have more inserts that updates I'd have thought it's more efficient to do an 'insert' first.
Roy Ernest
Roy Ernest
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8403 Visits: 6891
With MERGE that was introduced in SQL 2008, this article is actually obsolete unfortunately. But I am glad you read the article.
The issue with inserting first is that, you have to catch the PK error to find out if it should do an update. That would mean you will be doing an update on the table in the CATCH part of your code.

-Roy
paul s-306273
paul s-306273
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3466 Visits: 1169
Roy Ernest (1/11/2012)
With MERGE that was introduced in SQL 2008, this article is actually obsolete unfortunately. But I am glad you read the article.
The issue with inserting first is that, you have to catch the PK error to find out if it should do an update. That would mean you will be doing an update on the table in the CATCH part of your code.


Hi Roy - yes I've done the insert first/update second process several times in Oracle (dead easy).

I don't ever remember needing it in SQL Server.

Paul, UK.
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