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
                  )
earwig75Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
<air code>  If you're inserting static values, you can use EXISTS to determine if the row exists, and insert if it is not.
IF NOT EXISTS (SELECT SELECT ID, hello FROM myTable WHERE ID = 5 AND AccessID = 'hello') 
   INSERT INTO myTable (ID, theName, Auth, LastModifiedBy) 
   VALUES (5 ,'hello' ,'yes' ,'bob')      

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
INSERT INTO myTable
                  ([ID],[theName],[Auth],[LastModifiedBy])
            SELECT ID, AccessID, Auth, LastModifiedBy
            FROM (
                  SELECT 5 AS ID
                  ,'hello' AS AccessID
                  ,'yes' AS Auth
                  ,'bob' AS LastModifiedBy
            ) AS new_data
            WHERE NOT EXISTS (
                SELECT 1
                FROM myTable mt
                WHERE
                    mt.ID = new_data.ID AND
                    mt.theName = new_data.AccessID )
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.