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')
Begin
insert into REP (NAME,CODE) values ('SALIH','SALIH')
End

Solution 2

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.