Solved

Compare data in two tables

Posted on 2006-06-10
9
3,376 Views
Last Modified: 2009-09-07
I have two tables

table a on server a
and
table a on server b

is there a way to compare the data from each other and report the differences.....and what column this is.

A bit complex i know but i was hoping there was a way...

I know how to compare two columns in one table.
0
Comment
Question by:TRACEYMARY
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 15

Assisted Solution

by:dave4dl
dave4dl earned 100 total points
ID: 16879250
I have done this before and it isnt too hard.

Create an application that accesses both databases with an oledb driver (or odbc).  My app compared all tables on two different databases (or two different connections, they could be different databases on the same server) so the tables must be named the same.  All columns must also be named the same and tables you are comparing must have the same primary keys (or one could have no key defined and the key for the other table is assumed).  Also, matching columsn must have similar data types (i used date/time,number,text, and "other" as datatype classifications for "similar" data types).

for efficiency's sake, i pulled all the data into arrays (actually i created a custom array class so i could "index" my arrays but that isnt necessary for you to do if you are just running this once and dont have a lot of data).

Then compare records that have matching primary key values.

If you dont want to worry about pulling the data local, you could just issue sql statements and compare the resulting recordsets (pull one recordset and issue a sql query to the other database for each record in that set).

One note: You may want to put a tolerance in when comparing dates because some databases are more precise than others (or have more gradations in the precision of types).  This may make sense for numbers in some cases.

Sorry I can't post any code, it all belongs to the client i did the work for.

Good luck!
0
 
LVL 34

Assisted Solution

by:jefftwilley
jefftwilley earned 100 total points
ID: 16879286
Hi TraceyMary,
Dave is right about bringing the two tables together in one place. One way to do this is to link them into your database. You say they are on two different servers, do you have access to them with some kind of mapped drive setup? Are they Access databases or some other type? Once you find the differences between the two if there are any, how do you want the results displayed? Just some questions that need answering before we can help you much more.
J
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 100 total points
ID: 16879620
TRACEYMARY,
> is there a way to compare the data from each other and report the
> differences.....and what column this is.


Use the DB comparison tools such  as Red-Gate SQL compare, Apex SQL Diff etc, Which will show you difference in Table structure
0
 
LVL 15

Expert Comment

by:dave4dl
ID: 16879928
aneeshattingal,
I think Traceymary was looking for differences in data, not the structure
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Assisted Solution

by:mnrz
mnrz earned 50 total points
ID: 16879940
Hi

I dont know which database you are using but if you are using Oracle, there is a tool with name "TOAD for Oracle"  this tool has everything you cant imagine. it compares two tables, database and even create for you a Sql Script to make one table or database as same another.

regards
mohammad
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16880185
If you are looking to compare the data use the SQL Data Compare tool from

http://www.red-gate.com/products/SQL_Data_Compare/index.htm
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 150 total points
ID: 16880287
tracey

yes you can do this...

its basically a FULL OUTER JOIN
that you need between the two tables..

you can use (SQL SERVER) the system views INFORMATION_SCHEMA.COLUMNS to tailor
/ generate some comparison sql...

basically

Select case when a.pk is null  then 'New B'
                  when b.pk is null then 'NEW A'
                  else 'Changed' end as [Type]
        ,Coalesce(a.pk,b.pk) as PK
        ,...

 from [serverA].[databaseA].[ownerA].tablea as A
 full outer join [serverb].[databaseb].[ownerb].tableA as B
  on a.pk=b.pk
 Where A.cola <> b.cola
      or A.colb <> b.colb
     or ...

 order by 2


you have to decide how you want to identify changes...

you could have lots case statements in the select...

e.g.
  Case when A.Cola <> B.cola then ' Cola (' + convert(char(10),A.cola) + '/'+convert(char(10),b.cola)+')'  else '' end
 +   Case when A.Colb <> B.colb then ' Cola (' + convert(char(10),A.colb) + '/'+convert(char(10),b.colb)+')'  else '' end
+ ...

or just go with displaying both rows...

hth

 
0
 
LVL 7

Author Comment

by:TRACEYMARY
ID: 16880458
That some good ideas....did not think about doing in say asp read both recordsets.
That i can do quite easily......
the tool sounds good

This is probably not the first time i had to do this...i did it before comparing a two gig database but i was just comparing record counts ....this time its actual data.

Let me play with it....thanks all
0
 
LVL 7

Author Comment

by:TRACEYMARY
ID: 16880467
i just looked at the red gate software it says databases i hope it goes down to tables...i am downloading it to see..

Thanks...that save me some time...in writing a hugh script.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

17 Experts available now in Live!

Get 1:1 Help Now