mssql insert if not exist else update Edit

Murugan Andezuthu Dharmaratnam | 29 August 2020 | 181

MSSQL query to insert row into the table if the item does not exist. If the item exist update the row.

Sample Script

IF NOT EXISTS (select * from SetupCountry where id = '91')  
SELECT 'insert'
else
select 'update' 

Above code which will return insert if id does not exist, it will return udpate if the id exist.

Here is the code with insert and update

IF NOT EXISTS (select * from SetupCountry where id = '91')  
insert into setupcountry(id,name) values('91','India')
else
update setupcountry set name='India' where id='91'