How to check if a record already exists before inserting a new one

I'm looking for a simple clean way to make an MS SQL statement that selects all of column1 and column2 from one table, and inserts them into another table as new records, but only if the values for both column1 and column2 do not already exist in the table the insertion is being attempted on. Essentially, I do not want any duplication of values where the value of either column1 OR column2 must contain a unique value, but both do not need to be unique. Hope this is an easy one. Thanks.
Who is Participating?

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

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.

best way is to put a unique constraints/indexes on the table.

then simply insert the rows and capture exception on duplicates.
you can either ignore or report the attempt as needed.
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Rename the obvious, and give this a whirl..
INSERT INTO target_table (Column1, Column2) 
SELECT s.Column1, s.Column2
FROM source_table s
  -- LEFT means make sure you have all rows in s ...
  LEFT JOIN target_table t ON s.Column1 = t.Column1 AND s.Column2 = t.Column2
-- ... where there is no match in the target table for both columns
WHERE t.Column2 IS NULL 

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
BobHavertyComhAuthor Commented:
Hi sdstuber. I was hoping to not raise any exceptions at all. Is there one semi complex SQL statement that can accomplish this without errors?
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

yes - you can, using something like Jim Horn posted.

The downside is the multiple accesses on the table means extra IO.
The upside is triggers on tables can be avoided which could mean less IO.
BobHavertyComhAuthor Commented:
I saw this which looks like it might do what I want. Does anybody know about this and will it work?
INSERT INTO table ( column1, column2, column3 )
    SELECT $column1, $column2, $column3
       SELECT 1
         FROM table
        WHERE column1 = $column1
          AND column2 = $column2
          AND column3 = $column3 )

I think it's clo9se to what I want but it might not do exactly what I want. I don't fully understand it.
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Let us know if you're interested in trying the solution I posted, as I've used it in multiple projects, and can attest that it meets the requirements as stated in this question.
PortletPaulEE Topic AdvisorCommented:
>>"I think it's clo9se to what I want but it might not do exactly what I want. I don't fully understand it."

That is a very simple query using a method to test if there already is data matching 3 parameters

It is an alternative method to that proposed by Jim.

You should become familiar with EXISTS
BobHavertyComhAuthor Commented:
Thanks guys.
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
Query Syntax

From novice to tech pro — start learning today.