85

MySQL has this incredibly useful yet proprietary REPLACE INTO SQL Command.

Can this easily be emulated in SQL Server 2005?

Starting a new Transaction, doing a Select() and then either UPDATE or INSERT and COMMIT is always a little bit of a pain, especially when doing it in the application and therefore always keeping 2 versions of the statement.

I wonder if there is an easy and universal way to implement such a function into SQL Server 2005?

Rashed Hasan
  • 3,414
  • 5
  • 29
  • 71
Michael Stum
  • 167,397
  • 108
  • 388
  • 523

4 Answers4

59

This is something that annoys me about MSSQL (rant on my blog). I wish MSSQL supported upsert.

@Dillie-O's code is a good way in older SQL versions (+1 vote), but it still is basically two IO operations (the exists and then the update or insert)

There's a slightly better way on this post, basically:

--try an update
update tablename 
set field1 = 'new value',
    field2 = 'different value',
    ...
where idfield = 7

--insert if failed
if @@rowcount = 0 and @@error = 0
    insert into tablename 
           ( idfield, field1, field2, ... )
    values ( 7, 'value one', 'another value', ... )

This reduces it to one IO operations if it's an update, or two if an insert.

MS Sql2008 introduces merge from the SQL:2003 standard:

merge tablename as target
using (values ('new value', 'different value'))
    as source (field1, field2)
    on target.idfield = 7
when matched then
    update
    set field1 = source.field1,
        field2 = source.field2,
        ...
when not matched then
    insert ( idfield, field1, field2, ... )
    values ( 7,  source.field1, source.field2, ... )

Now it's really just one IO operation, but awful code :-(

Community
  • 1
  • 1
Keith
  • 133,927
  • 68
  • 273
  • 391
  • Great, Thanks! Saves the Select and often does not even need a teransaction in situations where i can be sure that between the Update and "my" insert, there is no other insert for that key. – Michael Stum Sep 20 '08 at 15:41
  • 2
    @Michael You better have a unique index on this table and handling for duplicate key errors if you are going to use this solution. – Sam Saffron Jan 05 '09 at 01:55
  • 3
    @Keith Your merge statement doesn't work. `MERGE` does not support the `WHERE` clause, you have to rewrite that using `USING` and `ON`. Also, unless you add `WITH (HOLDLOCK)`, there's a race and concurrent `INSERT`s might happen, with one of them failing due to the key clash. – Evgeniy Berezovsky Jul 12 '13 at 05:32
  • Yes, as pointed out here: http://weblogs.sqlteam.com/dang/archive/2009/01/31/UPSERT-Race-Condition-With-MERGE.aspx MERGE is not atomic. It takes out an implicit update lock, but releases it before performing an insert, which causes a race condition that can result in primary key violations. You must use an explicit HOLDLOCK in addition to the implicit UPDLOCK in order for the operation to be atomic. As it stands, it is not atomic, despite appearing to be a single statement. – Triynko Nov 09 '13 at 08:47
  • 1
    The MERGE syntax is wrong and it is fixed in a more recent answer from the same author: http://stackoverflow.com/a/243670/24472 – Larry Nov 21 '13 at 07:07
  • The above insert, followed by an update can still fail. With a unique key you can get duplicate key exceptions on the insert. I recommend adding to your update statement: UPDATE table WITH (SERIALIZABLE) SET ... – Elan Oct 23 '14 at 13:56
20

The functionality you're looking for is traditionally called an UPSERT. Atleast knowing what it's called might help you find what you're looking for.

I don't think SQL Server 2005 has any great ways of doing this. 2008 introduces the MERGE statement that can be used to accomplish this as shown in: http://www.databasejournal.com/features/mssql/article.php/3739131 or http://blogs.conchango.com/davidportas/archive/2007/11/14/SQL-Server-2008-MERGE.aspx

Merge was available in the beta of 2005, but they removed it out in the final release.

Karl Seguin
  • 20,450
  • 5
  • 40
  • 49
17

What the upsert/merge is doing is something to the effect of...

IF EXISTS (SELECT * FROM [Table] WHERE Id = X)
   UPDATE [Table] SET...
ELSE
   INSERT INTO [Table]

So hopefully the combination of those articles and this pseudo code can get things moving.

Dillie-O
  • 28,071
  • 14
  • 93
  • 139
10

I wrote a blog post about this issue.

The bottom line is that if you want cheap updates and want to be safe for concurrent usage, try:

update t
set hitCount = hitCount + 1
where pk = @id

if @@rowcount < 1 
begin 
   begin tran
      update t with (serializable)
      set hitCount = hitCount + 1
      where pk = @id
      if @@rowcount = 0
      begin
         insert t (pk, hitCount)
         values (@id,1)
      end
   commit tran
end

This way you have 1 operation for updates and a max of 3 operations for inserts. So, if you are generally updating, this is a safe cheap option.

I would also be very careful not to use anything that is unsafe for concurrent usage. It's really easy to get primary key violations or duplicate rows in production.

Tanjim Khan
  • 556
  • 1
  • 7
  • 17
Sam Saffron
  • 121,058
  • 74
  • 309
  • 495