Solved

T-SQL: Comparing 2 tables

Posted on 2007-12-04
3
2,231 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

23 Experts available now in Live!

Get 1:1 Help Now