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:
- 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 I am checking for the Name and First Name of a person and if it exists it will replace it else insert it.