SQL Server 2005 implementation of MySQL REPLACE INTO?

MySQL has this incredibly useful yet properitary REPLACE INTO SQL Command. I wonder: 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 a pain in the a.., 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 question and answers originated from www.stackoverflow.com
Question by (8/1/2008 10:13:34 PM)

Answer

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 into tablename 
where idfield = 7
when matched then
    update
    set field1 = 'new value',
        field2 = 'different value',
        ...
when not matched then
    insert ( idfield, field1, field2, ... )
    values ( 7, 'value one', 'another value', ... )

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

Answer by

Find More Answers
Related Topics  mysql  sql-server  query
Related Questions
  • SQL Server query replace for MySQL Instruction

    I'm new to SQL Server, and used mysql for a while now... SELECT A.acol, IF(A.acol<0,"Neg","Pos") as Column2 From Table I want to do something like that on SQL Server, but there doesn't exi…
  • MySQL to SQL Server 2005

    How can I convert a database from MySQL to MS SQL Server 2005?
  • SQL Server version of MYSQL insert into

    I have the following query running on a MySQL, but it fails when I run it on a SQL Server database. How should it look to please SQL Server? INSERT INTO first_table (pk, data) VALUES ((SELECT val…
  • MySQL queries in SQL-Server?

    How can we best use our existing MySQL queries with SQLserver? Alternately, what type of changes do we need when we are converting our database server from MySQL to SQLServer?
  • Replace SQL Server 2005 cursor

    Here is the simplified cursor: SET IMPLICIT_TRANSACTIONS ON SET @curTemp = CURSOR FAST_FORWARD FOR SELECT gpno, ssn FROM EligCov Group BY gpno, ssn OPEN @curTemp -- loop through cursor and bu…
  • Synchronizing SQL Server 2005 with MySQL

    I'm need to copy several tables wholesale from a third-party SQL Server 2000 database to a MySQL 5 database and keep them synchronized--i.e., when some CRUD happens on the SQL Server tables, I'd lik…
  • SQL Server 2005 to MySQL connection

    I want to be able to email a report daily from a glpi database in MySQL. I would like to create a SSIS job to pull data from MySQL. How do I do this?
  • Convert SQL Server query to MySQL

    Possible Duplicate: Select TOP X (or bottom) percent for numeric values in MySQL How would I write this query in MySQL?? SELECT TOP 50 PERCENT * FROM Persons The number of entrie…
  • Update query on linked MySQL table from SQL Server

    I have a MS SQL Server with a linked MySQL server. I need to partially synchronize a table between the two servers. This is done in three steps and based on a condition: Delete all rows from th…
  • SQL Server 2005 SUM

    Hey all, this is my query string here: SELECT SUM(Total) as Total, AdministratorCode, SUM(WOD.Quantity) as thePass FROM tblWO as WO, tblWOD as WOD WHERE WOD.OrderID = WO.ID AND WO.Ord…