简体   繁体   中英

Compare data between two similar tables in different servers

I need to compare data in two tables. These tables are similar in schema but will have different data values. I want to export these data to csv or similar format and then check for differences.

I would like to perform this check with a python script. I have already figured out how to export the data to csv format. But my problem is the since the two tables are not in sink the primary keys may be different for the same row. Also the row order may be different in the to tables. CSV compare will not help me in this aspect.

Example database tables in CSV format are below id,name,designation,department

Table employee in db1

1,Ann,Manager,Sales

2,Brian,Executive,Marketing

4,Melissa,Director,Engineering

5,George,Manager,Plant

Table employee in db2

1,Ann,Manager,Sales

2,George,Manager,Plant

3,Brian,Executive,Marketing

Here Melissa is a missing record in the second DB. But George and Brian even though they have different Id's are considered the same record.

I've found that there are commercial software for this task, but what i need is a script that can be used in a process flow to identify the differences in the tables.

I have such question before. My solution as below:

  1. Connect both database with Database Link.
  2. Then join both table with the primary key.
  3. compare the column of both table. eg "SCHEMAA".T1.col1<>"SCHEMAB"T1.col1

Might not be the complete solution, hoping this might be helpful.

If the two tables are to be compared, which resides in different DB's/environments you require a DB_Link for it in SQL_Developer.

select name||'--'||designation||'--'||department from employee_db_1
minus
select name||'--'||designation||'--'||department from employee_db_2
union 
select name||'--'||designation||'--'||department from employee_db_2
minus
select name||'--'||designation||'--'||department from employee_db_1;

Good Article: https://blogs.oracle.com/sql/how-to-find-and-delete-duplicate-rows-with-sql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM