Solved

T-SQL: Comparing 2 tables

Posted on 2007-12-04
3
2,235 Views
Last Modified: 2012-05-05
when comparing 2 tables, is there a way to return the spicific column that changed?
Currently I am successfully comparing the tables using this code:
SELECT MIN(TableName) AS TableName, RateNumber, UpdatedBy, UpdatedOn
	FROM
	(
 
	SELECT 'Current Data' AS TableName, tar_number AS RateNumber, tar_updateby AS UpdatedBy, tar_updateon AS UpdatedOn
	FROM tariffheader
 
	  UNION ALL
 
	SELECT 'Original Snapshot' AS TableName, RateNumber, UpdatedBy, UpdatedOn
	FROM TarrifSnapshotTemp
	) tmp
 
	GROUP BY RateNumber, UpdatedBy, UpdatedOn
	HAVING COUNT(*) = 1
	ORDER BY RateNumber

Open in new window

0
Comment
Question by:pablo_munoz
[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
3 Comments
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20406268
SELECT MIN(TableName) AS TableName, RateNumber, UpdatedBy, UpdatedOn,
 case when min(RateNumber) <> max(RateNumber) then 'YES' else '' end as RateNumberChanged,
 case when min(UpdatedBy) <> max(UpdatedBy) then 'YES' else '' end as UpdatedByChanged,
 case when min(UpdatedOn) <> max(UpdatedOn) then 'YES' else '' end as UpdatedOnChanged
        FROM
        (
 
        SELECT 'Current Data' AS TableName, tar_number AS RateNumber, tar_updateby AS UpdatedBy, tar_updateon AS UpdatedOn
        FROM tariffheader
 
          UNION ALL
 
        SELECT 'Original Snapshot' AS TableName, RateNumber, UpdatedBy, UpdatedOn
        FROM TarrifSnapshotTemp
        ) tmp
 
        GROUP BY RateNumber, UpdatedBy, UpdatedOn
        HAVING COUNT(*) = 1
        ORDER BY RateNumber
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20406296
Actually, can you please explain
"currently I am successfully comparing the tables using this code"

What is it successfully doing? It looks like it's showing you the rows that exist only in Current or Shapshot.  Is there an ID that links them together?
0
 
LVL 3

Author Comment

by:pablo_munoz
ID: 20406354
Actually it's the same data, I don't have the ability to add a trigger to the table that I need to check, so instead I am selecting the data into a temp table, once I have a snapshot of the current data, I can then compare the two tables and check for differences. if there is a difference, I email the concerned party. I also update the temp table if there is a difference.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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