If Exists Update Else Insert

Code Title: If Exists Update Else Insert
Language: SQL Server
Version: 2000
Database: MS SQL

Description:

This is a very common practice when performing some validation before inserting the data to avoid duplicates. Below is the example.

Added: 10/31/2008
Profile: Syed
User Name: syed

Recent Entries by Author:
Coming Soon!

Note: Please, always give credit to author for his/her work, if used in production environment.

Code


IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
UPDATE Table1 SET (...) WHERE ColumnName='Value'
ELSE
INSERT INTO Table1 VALUES (...)