how can i find differnces between two tables - from two database - in t-sql ?

Hello ,

I've 2 databases : first and second.
first has a table person with an id
second has a table user with an id.

we have differences between the two databases.

my question
how can i have all the id that are in first and not in second ?
how can i have all the id that are in second and not in first ?

thank you in advance for you help.

Erwin PombettAsked:
Who is Participating?
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 Developer, Architect, and AuthorCommented:
>how can i have all the id that are in first and not in second ?
Simple query with a LEFT JOIN (return all person p), and a WHERE to filter out those where no matching user.
FROM first.dbo.person p
   LEFT JOIN second.dbo.user u ON =

Open in new window

>how can i have all the id that are in second and not in first ?
Just flip it around
FROM second.dbo.user u
   LEFT JOIN first.dbo.person p ON =

Open in new window

There are a couple of other ways to do this, using IN and CROSS JOIN.

Btw the id's for people and user are likely the people id and user id, so you may have to look for foreign keys and use those. For example, table u has a person_id that correctly maps to the person table.

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
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 2008

From novice to tech pro — start learning today.

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.