Query two databases for duplicate entries

Posted on 2007-04-10
Last Modified: 2013-11-15
I have two SQL databases.  One is for Candidates and one is our Main database.  We have a sp that transfers a Candidate into our Main database once we make contact with them and then deletes them from the Candidate database.  Problem is, our sp to delete from the Candidate database is not working - so we have duplicate entries in both databases.

I need a query so I can look for duplicates in both databases.
Database 1 - CandSource  Table - Contact1
Database 2 - Goldmine   Table - Contact1

output to delimited file.
I know this is easy for a SQL god...but I am not one...
Thank you thank you.  
Question by:NetzillaKB
  • 2
  • 2

Accepted Solution

soco180 earned 500 total points
ID: 18884877
First you are going to have to define what a duplicate is...In the below query I assumed a duplicate means a person with the same last name and same first name. That is very loose logic. You might want to include city, state and zip.

How do you define a duplicate?

FROM CandSource..Contact1 C
INNER JOIN GoldMine..Contact1 G
    ON C.ContactLastName = G.ContactLastName
    AND C.ContactFirstName = G.ContactFirstName

Author Comment

ID: 18884933
the duplicate would be the name only.  Frequently, while making contact with the candidate, the recruiter updates their city,state, or zip and then they wouldn't be duplicates.

Thanks.  I'll see if this works...KB

Author Comment

ID: 18885007
I had to tweak the table names a little, but I'm getting an output to the screen.  How can I see which database the entry CAME from?  And how can I get the output to a delimited file?

Expert Comment

ID: 18885124
Another question, do you have a Primary Key such as an Identity column on the Candidate DB? It might be easier for you to script you Delete statement.

As for the delimited part: Are you using MS SQL Query Analyzer or MS SQL Server Managment Sudio 2005 or something else?

I tried to clear that up below by only selecting contacts from you Candidate database (which are also in the GoldMine DB).

SELECT C.ContactLastName + ',' + C.ContactFirstName AS Name
FROM CandSource..Contact1 C
INNER JOIN GoldMine..Contact1 G
    ON C.ContactLastName = G.ContactLastName
    AND C.ContactFirstName = G.ContactFirstName

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now