If Record Exists, Update Else Insert

doubleplusgood picture doubleplusgood · Oct 27, 2009 · Viewed 11.9k times · Source

I'm trying to move some data between two SQL Server 2008 tables. If the record exists in Table2 with the email from Table1 then update that record with the data from Table1, else insert a new record.

In Table1 I have a number of columns; first name, surname, email and so on.

I'm not quite sure how to structure the query to update Table2 if the email from Table1 exists or insert a new row if email from Table1 does not exist in Table2.

I tried doing a few searches on Google but most solutions seem to work by creating some stored procedure. So I wondered if anyone might know how to build a suitable query that might do the trick?

Answer

APC picture APC · Oct 27, 2009

I think MERGE is what you want.