Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Comparison Query

Author  Topic 

gangadhar.kamaraj
Starting Member

3 Posts

Posted - 2014-10-13 : 13:49:33
Hi All,

I am newbie to SQL query. I have got a requirement to compare data between 2 different tables and provide the mismatch of the destination table comparing it against the Source table. Let me specify it more clearly with an example.

Source is considered as 'Table1' in 'ServerX'
Destination is considered as 'Table2' in 'ServerY'
Both these 2 tables have different ColumnNames containing the same data like 'Street Address' (in 'Table1') vs 'Living Place' (in 'Table2') for Employees
Each table may different information stored in both these tables for Employees like 'Table1' can have 'Salary' as an Column; whereas 'Table2' might not have that information
We are expected to compare the data that is present in 'Table1' with 'Table2' for each employee record and get the differentials that doesn't match the source
Both these tables have 'Employee ID' as common (with different column names) in both of them

I understand that there are multiple tools available online to get this done, but we are in need to build a SQL query to get this done. Can someone help me with what functions can be used to build the query, so that I can try them?

Thanks in advance for all your suggestions

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-10-13 : 14:00:07
Please show us sample data and expected output to make your question clear.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-10-13 : 14:01:03
To use a T-SQL query to do this, you will either need to import one of the tables from one server to the other, so both tables are in the same server. Alternatively, you can make a linked server connection from one server to the other and use linked server queries.

Regardless of which way you choose, one way to find the differences would be to use the EXCEPT construct. For example:
SELECT col1, col2 FROM Table1
EXCEPT
SELECT colA,colB from Table2;

-- and the other way around
SELECT colA,colB from Table2
EXCEPT
SELECT col1, col2 FROM Table1;
Go to Top of Page

gangadhar.kamaraj
Starting Member

3 Posts

Posted - 2014-10-13 : 14:14:36
quote:
Originally posted by tkizer

Please show us sample data and expected output to make your question clear.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/

Hi TKizer, Please find below the example:

Table1 'Person' on Database 'A' hosted on Server 'X'. Below is the table content:
Emp ID,First Name,Last Name,Manager Name,Department,Job Code,Title
100,Raja,Rajan,Mark Thomas,Security,CB001,Senior Analyst
101,Rani,Maria,Steve Cook,Reception,CB005,Receptionist
102,John,Titto, Mark Gill,Administration,CB500,Junior

Table2 'ADSAccount' on Database 'B' hosted on Server 'Y'. Below is the table content:
Resource ID,Given Name,Sur Name,Supervisor,Title
100,Raja,Rajan,Mark Thomas,Analyst
101,Rani,Maria,Steve Cook,NULL
102,John,Titto,Mark Gill,Junior

Expected Output
Emp ID,First Name,Last Name,Manager Name,Department,Job Code,Title
100,NULL,NULL,NULL,NULL,NULL,Senior Analyst
101,NULL,NULL,NULL,NULL,NULL,Receptionist

Go to Top of Page

gangadhar.kamaraj
Starting Member

3 Posts

Posted - 2014-10-13 : 14:27:28
quote:
Originally posted by James K

To use a T-SQL query to do this, you will either need to import one of the tables from one server to the other, so both tables are in the same server. Alternatively, you can make a linked server connection from one server to the other and use linked server queries.

Regardless of which way you choose, one way to find the differences would be to use the EXCEPT construct. For example:
SELECT col1, col2 FROM Table1
EXCEPT
SELECT colA,colB from Table2;

-- and the other way around
SELECT colA,colB from Table2
EXCEPT
SELECT col1, col2 FROM Table1;

Hi James,
Thanks for your response. I will try working this out. But, we will not be able to load the data into a table in the same server, hence I might need to use linked server queries to get this done.

Also, got a question. Will using an EXCEPT clause list down the differences in data as well for the destination table by comparing it against the source table?

Go to Top of Page
   

- Advertisement -