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.
LVL 9
BobHavertyComhAsked:
Who is Participating?
 
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

0
 
sdstuberCommented:
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.
0
 
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?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
sdstuberCommented:
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.
0
 
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
     WHERE NOT EXISTS (
       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.
0
 
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.
0
 
PortletPaulfreelancerCommented:
>>"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
0
 
BobHavertyComhAuthor Commented:
Thanks guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.