Skip to content Skip to sidebar Skip to footer

Insert Into Ignore Duplicates That Are Not The Primary Key

I'm trying to find a query for sql that will just insert values but not do it should the value exist. Now ive seen alot of examples but they all rely on primary keys or table to ta

Solution 1:

You should create a Unique Constraint composed by the three fields (txtLastName, txtEmail, txtZip).

The links directs you to SQL Server docs, but the concept of unique constraint is RDBMS universal.

Just beware that when you create a Unique Constraint, your duplicate insert will not just fail silently: it will throw an error saying the insert tried to violate the unique constraint. And, of course, it should do that! Make sure your code handles that exception.

Solution 2:

Try this:

INSERTINTOtable (txtLastName,txtEmail,txtZip) 
SELECT'Tester','test@test.com','12345'WHERENOTEXISTS (SELECT*FROMtableWHERE txtLastName ='Tester'AND txtEmail ='test@test.com'AND txtZip ='12345' 
)

Post a Comment for "Insert Into Ignore Duplicates That Are Not The Primary Key"