Avatar of earwig75
earwig75
 asked on

Skip insert in SQL if duplicate exists.

I want to skip or ignore an insert if the values already exist in the table. Below is the SQL I am trying to use but it doesn't seem to work. Can someone assist? I am filling in the values, but they are normally entered by variables in my application and this query is in a loop... that's why I want to ignore if a duplicate exists. Thank you.

INSERT INTO myTable
                  ([ID],[theName],[Auth],[LastModifiedBy])
            VALUES(
                  5
                  ,hello
                  ,yes
                  ,bob
                  )      
            WHERE hello
            NOT IN      (SELECT [ID], [hello]
                         FROM myTable
                         WHERE ID = 5
                         AND AccessID = hello
                  )
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jim Horn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Scott Pletcher

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Your help has saved me hundreds of hours of internet surfing.
fblack61