[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Compare data in two tables

Posted on 2006-06-10
9
Medium Priority
?
3,425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 15

Assisted Solution

by:dave4dl
dave4dl earned 400 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 400 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 400 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 15

Expert Comment

by:dave4dl
ID: 16879928
aneeshattingal,
I think Traceymary was looking for differences in data, not the structure
0
 
LVL 8

Assisted Solution

by:mnrz
mnrz earned 200 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 600 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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