[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3429
  • Last Modified:

Compare data in two tables

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
TRACEYMARY
Asked:
TRACEYMARY
  • 2
  • 2
  • 2
  • +3
5 Solutions
 
dave4dlCommented:
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
 
jefftwilleyCommented:
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
 
Aneesh RetnakaranDatabase AdministratorCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dave4dlCommented:
aneeshattingal,
I think Traceymary was looking for differences in data, not the structure
0
 
mnrzCommented:
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
 
Aneesh RetnakaranDatabase AdministratorCommented:
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
 
LowfatspreadCommented:
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
 
TRACEYMARYAuthor Commented:
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
 
TRACEYMARYAuthor Commented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now