Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

How do I find out the differences in data between the two tables that have exact schema, and how to produce synchronisation SQL to get the union results (without duplicates) ?

These are the 2 tables:

SOURCE01.dbo.Customers (31,022 rows)

TARGET01.dbo.Customers (29,300 rows)

The schema of each table is :

  • [CustomerId] : nvarchar(255)
  • [CustomerSerializedProfile]: nvarchar(max)
  • [CreatedDatetime] : DateTime
share|improve this question

3 Answers 3

Using Native tools :

tablediff : the tablediff utility compares the data in the source table to the table in the destination table.

powershell : compare-object allows you to achieve that. here is a good example

third party:

redgate schema and data compare. You can even use powershell and schema/data compare to automate things.

share|improve this answer

I've used this one recently for a similar purpose:

select
    s.*
    ,t.*
from SOURCE01.dbo.Customers as s
full outer join TARGET01.dbo.Customers as t
    on s.CustomerId = t.CustomerId
where s.CustomerSerializedProfile <> t.CustomerSerializedProfile
or s.CreatedDatetime <> t.CreatedDatetime
or s.CustomerId is NULL
or t.CustomerId is NULL;

(Code edited post comment; apologies, I was wining it without an instance to hand.)

It does rely on the primary key being consistent. But you have to have something consistent after all. A meta script to generate code like the above is relatively easy to write and makes many-column tables easy to compare.

As for synchronisation you will have to source left join target and 'target left join source`, then decide what you want to do with the result of each.

share|improve this answer

This should give you the differences between the two tables, you can then wrap this in an insert query to put the differences from A into B or vice versa.

SELECT A.CustomerId, A.CustomerSerializedProfile, A.CreatedDatetime
  FROM SOURCE01.dbo.Customers A
 WHERE NOT EXISTS (SELECT B.ID
                 FROM TARGET01.dbo.Customers
                WHERE B.CustomerId= A.CustomerId
                  AND B.CustomerSerializedProfile= A.CustomerSerializedProfile
                  AND B.CreatedDatetime= A.CreatedDatetime)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.