sql server if exists update else insert 1

sql server if exists update else insert

if exists(SELECT * from Student where FirstName='Akhil' and LastName='Mittal')            
BEGIN            
 update Student set FirstName='Anu' where FirstName='Akhil'  
End                    
else            
begin  
insert into Student values(1,'Akhil','Mittal',28,'Male',2006,'Noida','Tenth','LFS','Delhi')  
end 

Here is what the above code is Doing:
1. It is checking if the record exists in the table.
2. If it exists, it will update the record.
3. If it doesn’t exist, it will insert the record.

Similar Posts