Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have two insert script files which has around 1,00,000 records in each script file. Both files will insert the records into one table only . i mean schema of table def is one and the same.

But these two files are different in version, that is Data/content in those might be different (different in records).

Now my job is to compare these two files data and find it out wheather they have different in colums data based on primary key column.

For this, i have create two temp tables with different tables names and try to run the insert script files on appropriate tables.

So could you please suggest me or guide me how to compare the data in both the files by inserting into temp table and compareing these temp table with common primary key value.

Please give the code (sql statement).

Thanks in advance..

Thanks & Regards
Chiranjeevi Ommi.
Posted
Comments
joshrduncan2012 29-Nov-12 17:05pm    
We won't "give" you anything. Have you tried this yet? If so, where did you get stuck?
chaau 29-Nov-12 19:42pm    
As a small guide, JEFT JOIN and RIGHT JOIN should be your starting points

1 solution

Here is a sample approach.
I have created two tables with identical columns and inserted some data in both the tables. Then i have queried the tables to retrieve rows which are not same.

SQL
CREATE TABLE #Table1
(
	ID INT PRIMARY KEY,
	[Name] VARCHAR(50),
	Email VARCHAR(50)
)

CREATE TABLE #Table2
(
	ID INT PRIMARY KEY,
	[Name] VARCHAR(50),
	Email VARCHAR(50)
)


INSERT INTO #Table1
SELECT 1, 'XYZ', 'XYZ@XYZ.com' UNION ALL
SELECT 2, 'XYZ 1', 'XYZ1@XYZ.com' UNION ALL
SELECT 3, 'XYZ 2', 'XYZ2@XYZ.com' UNION ALL
SELECT 4, 'XYZ 3', 'XYZ3@XYZ.com' UNION ALL
SELECT 5, 'XYZ 4', 'XYZ4@XYZ.com' 

INSERT INTO #Table2
SELECT 1, 'XYZ', 'XYZ@XYZ.com' UNION ALL
SELECT 2, 'XYZ 1', 'XYZ13@XYZ.com' UNION ALL
SELECT 3, 'XYZ 2', 'XYZ2@XYZ.com' UNION ALL
SELECT 4, 'XYZ 3', 'XYZ3@XYZ.com' UNION ALL
SELECT 5, 'XYZ 45', 'XYZ4@XYZ.com' 

--Query to list out rows where ID is same in Table1 and Table2, but the Name or Email is different
SELECT * FROM #Table1 T1 
LEFT JOIN #Table2 T2 ON T1.ID = T2.ID
WHERE T1.[Name] <> T2.[Name] OR T1.Email <> T2.Email


DROP TABLE #Table1
DROP TABLE #Table2


Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900