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 EmployeesEach 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 informationWe 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 sourceBoth these tables have 'Employee ID' as common (with different column names) in both of themI 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 KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
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 Table1EXCEPTSELECT colA,colB from Table2;-- and the other way aroundSELECT colA,colB from Table2EXCEPTSELECT col1, col2 FROM Table1; |
|
|
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 KizerSQL Server MVP since 2007http://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,Title100,Raja,Rajan,Mark Thomas,Security,CB001,Senior Analyst101,Rani,Maria,Steve Cook,Reception,CB005,Receptionist102,John,Titto, Mark Gill,Administration,CB500,JuniorTable2 'ADSAccount' on Database 'B' hosted on Server 'Y'. Below is the table content:Resource ID,Given Name,Sur Name,Supervisor,Title100,Raja,Rajan,Mark Thomas,Analyst101,Rani,Maria,Steve Cook,NULL102,John,Titto,Mark Gill,JuniorExpected OutputEmp ID,First Name,Last Name,Manager Name,Department,Job Code,Title100,NULL,NULL,NULL,NULL,NULL,Senior Analyst101,NULL,NULL,NULL,NULL,NULL,Receptionist
|
|
|
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 Table1EXCEPTSELECT colA,colB from Table2;-- and the other way aroundSELECT colA,colB from Table2EXCEPTSELECT 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?
|
|
|
|
|
|