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, 8:37 AM
Thanks very usefull
Roshan KumarPosted Jun 2, 2021, 12:00 PM
This works with multiple rows????
pertti jarvaPosted Jan 21, 2015, 11:07 AM
You could also just do the update and after that check if @@rowcount = 0 and do the insert in that case.