Solved

Table data comparison script

Posted on 2008-09-29
6
389 Views
Last Modified: 2012-06-21
I'm looking for a script that will allow me to compare two identical tables lets call the table A and table B in different databases.  Table A can be the master table.

1. The script ideally must shows rows where additional rows exists A which are not in B.
2. Where additional rows exist in B but not in A.
3. Also matching on a primary key it must show rows where values in any columns differ between rows.
0
Comment
Question by:dfincham28
[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
6 Comments
 
LVL 11

Assisted Solution

by:dready
dready earned 100 total points
ID: 22600152
1:
select * from tblA where tblA.Id not in (select id from  tblB)
2:
select * from tblB where tblB.id not in (select id from tblA)
3. Depends on how many columns you have... lets say that except for the PK column (ID) you hava colA, colB and colC

select * from tblA inner join tblB on tblA.ID = tblB.ID where tblA.colA <> tblB.ColA or tblA.colB <> tblB.ColB or tblA.colC <> tblB.colC
0
 
LVL 27

Accepted Solution

by:
Zberteoc earned 350 total points
ID: 22600892
Lets say your tables have the following structure:

id - PK,
col1,
col2,
...
colN
select 
	*
from 
	db1..tableA as A
	full join db2..tableB as B
		on B.id=A.id
where
	A.id is null
	or 
	B.id is null
	or 
	( 
		A.id=B.id 
		and 
		(
			isnull(A.col1,<def_val_col1_type>)<>isnull(B.col1,<def_val_col1_type>)
			or
			isnull(A.col2,<def_val_col2_type>)<>isnull(B.col2,<def_val_col2_type>)
			or
				...
			isnull(A.colN,<def_val_colN_type>)<>isnull(B.colN,<def_val_colN_type>)
		)
	)

Open in new window

0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 50 total points
ID: 22601764
scripts are good until you find professional tools like http://www.red-gate.com/products/SQL_Data_Compare/index.htm
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dfincham28
ID: 22614549
The problem with professional tools is whenever you need to do the data comparison be it at a client site you will need to have that tool installed?
0
 

Author Comment

by:dfincham28
ID: 22657885
SQL Delta is a better professional tool
0
 

Author Comment

by:dfincham28
ID: 22877449
Someone at work has solved the problem for me
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Display Date and Time 7 47
Errror when importing data from Oracle to SQL 6 62
Database Availability Group Distribution 9 45
What is needed to become a DBA? 7 46
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

738 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