Solved

Table data comparison script

Posted on 2008-09-29
6
385 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
6 Comments
 
LVL 11

Assisted Solution

by:dready
dready earned 100 total points
Comment Utility
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 26

Accepted Solution

by:
Zberteoc earned 350 total points
Comment Utility
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 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 50 total points
Comment Utility
scripts are good until you find professional tools like http://www.red-gate.com/products/SQL_Data_Compare/index.htm
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:dfincham28
Comment Utility
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
Comment Utility
SQL Delta is a better professional tool
0
 

Author Comment

by:dfincham28
Comment Utility
Someone at work has solved the problem for me
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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

9 Experts available now in Live!

Get 1:1 Help Now