insert row if not exit -MSSQL

How to insert a row in MS SQL if the row already not inserted. There are two approach for this task

Solution 1

If Not Exists(select * from REP where NAME=? AND CODE='SALIH')
insert into REP (NAME,CODE) values ('SALIH','SALIH')

Solution 2

insert into REP (code,NAME)
Select 'SAFVAN1','SAFVAN' Where not exists(select * from REP where code='SAFVAN1' AND NAME='SAFVAN')

Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.