Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Tuesday, September 29, 2009

SQL - Compare Records in Two Tables

I recently needed to compare records in a Production database with that of our Development Enviroment.  Some recent learning on the topic of Unions and the use of the MINUS (Oracle) and EXCEPT (SQLServer) operators has led me to the following example:

(SELECT * FROM old_table
 MINUS
 SELECT * FROM new_table)
UNION ALL
(SELECT * FROM new_table
 MINUS
 SELECT * FROM old_table)
Oracle SQL by Example. Rischert. ISBN: 0-13-145131-6. pg. 405-6

This searches for missing records two tables (in both directions) then merges the results.  This could replace some custom logic that reads records sequentially for a farily elegant SQL solution.

For SQL Server users (like me!) replace the MINUS operator with EXCEPT.

Happy Coding.