Thursday, April 15, 2010

EXCEPT,INTERSECT

I have been working a project where in data needs to archived from the main reporting database at ed of each month. Once the data has been archived, I run a validation stored procedure to check the counts in the archive and the main source table. Once the validation is complete, a message is sent out to the developers indicating how many matches and/or mis matches are found in the data. I noticed the validation process was taking too long to report the results since the number of checks that were to made were on a small set of data. Here is where i tried the use of INTERSECT and EXCEPT clause available in SQL Server 2008. The results were generated very quickly since these were set based operations, earlier i was using the cursor approach which was taking too long to report the comparison results. Here is an example of the intersect and except clauses. For the matches I used the INTERSECT clause:

SELECT SummaryTable,cnt FROM #tmp_Arch

INTERSECT
SELECT TableName,cnt FROM #tmp_sumry

For the rows which were not macthing I used the EXCEPT clause.

SELECT SummaryTable,cnt FROM #tmp_Arch

EXCEPT
SELECT TableName,cnt FROM #tmp_sumry

No comments:

Post a Comment