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


Uday DodiyaPosted Sep 3, 2022, 4:37 AM EDT
Thanks very usefull
Roshan KumarPosted Jun 2, 2021, 8:00 AM EDT
This works with multiple rows????
pertti jarvaPosted Jan 21, 2015, 6:07 AM EST
You could also just do the update and after that check if @@rowcount = 0 and do the insert in that case.