If Exists then Update else Insert in SQL Server

Introduction
 
In this blog I'll tell you about how to check and then select whether to update or insert in table in SQL Server.
 
I am providing an example by which you can achieve this:
  1. if exists(SELECT * from Student where FirstName='Akhil' and LastName='Mittal')            
  2. BEGIN            
  3.  update Student set FirstName='Anu' where FirstName='Akhil'  
  4. End                    
  5. else            
  6. begin  
  7. insert into Student values(1,'Akhil','Mittal',28,'Male',2006,'Noida','Tenth','LFS','Delhi')  
  8. end 
 Here I am checking for the Name and First Name of a person and if it exists it will replace it else insert it.
Ebook Download
View all
Learn
View all