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?
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
existsand then theupdateorinsert)There’s a slightly better way on this post, basically:
This reduces it to one IO operations if it’s an update, or two if an insert.
MS Sql2008 introduces
mergefrom the SQL:2003 standard:Now it’s really just one IO operation, but awful code 🙁