Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to compare two tables on different databases


How to compare two tables on different databases

Author
Message
jain_abhishek
jain_abhishek
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 44
Hi I have two databases on same server and both databases.These do databases are same (we can say replica) .There is a table name 'Transactions' on both databases.Now I have to create a job which runs on daily basis and check whether the data in Table 'Transaction' in both the databases is same or not (i.e to check whether table in both database is in sync) .How can I do this.Please suggest.
Andras Belokosztolszki
Andras Belokosztolszki
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1385 Visits: 1585
jain_abhishek (6/10/2009)
Hi I have two databases on same server and both databases.These do databases are same (we can say replica) .There is a table name 'Transactions' on both databases.Now I have to create a job which runs on daily basis and check whether the data in Table 'Transaction' in both the databases is same or not (i.e to check whether table in both database is in sync) .How can I do this.Please suggest.



you can create a checksum for the values in the table (see checksum http://msdn.microsoft.com/en-us/library/ms189788(SQL.90).aspx and cheksum_agg (http://msdn.microsoft.com/en-us/library/ms188920(SQL.90).aspx) and then compare the checksums. You can do this on a per row level (you may want to do this via a linked server), or the whole table. The fewer checksums you compare the less accurate the result will be (i.e. if you calculate a checksum for the whole table you may encounter cases where the checksums match, but the tables are different. You should also consider calculating the checksums based on some inside knowledge (e.g. do the checksums on only a few columns if that is sufficient to determine a difference). The checksum calculation may be expensive, so do some initial tests (like row counts). If you need to compare all the data without checksums, use either linked servers or there are third party tools too.

Regards,
Andras



Andras Belokosztolszki, MCPD, PhD
GoldenGate Software
hi_abhay78
hi_abhay78
SSC Veteran
SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)

Group: General Forum Members
Points: 269 Visits: 386
use tablediff utility : C:\Program Files\Microsoft SQL Server\90\COM
give source and destination [servername , tablename , databasename]..
simple to use (consult BOL).
make sure that the table has unique/primary key (clustered index)

HTH

Abhay Chaudhary
Sr.DBA (MCITP/MCTS :SQL Server 2005/2008 ,OCP 9i)
jain_abhishek
jain_abhishek
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 44
but both the tables are in different database.how can i compare
jain_abhishek
jain_abhishek
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 44
One more thing If both table are not having same data then how will I make both tablles in sync ie same data in both table
hi_abhay78
hi_abhay78
SSC Veteran
SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)SSC Veteran (269 reputation)

Group: General Forum Members
Points: 269 Visits: 386
its possible ........databases can be different ...
for your second query : its very much possible..
Very busy right now ....wil reply later ...till then do soem research :-)

Abhay Chaudhary
Sr.DBA (MCITP/MCTS :SQL Server 2005/2008 ,OCP 9i)
arup chakraborty
arup chakraborty
SSC-Addicted
SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)

Group: General Forum Members
Points: 458 Visits: 365
Since databases are different, use fully qualified name of the databases i.e. prefix the table name with server IP.database.user.table

To comapre the results, u can use EXCEPT operator.
loke
select * from d1..table1
except
select * from d2..table2
Randy-574768
Randy-574768
SSC Veteran
SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)

Group: General Forum Members
Points: 248 Visits: 267
After you get the first compare done, you could add an update_dt field and have it default to the current getdate() value. Then, if there are dates since the last comparison, then those are the ones that are different.

There's also merge replication, once you got them in a spot where they are the same.

The EXCEPT is the way I would make that first compare, too.

Randy
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search