Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server compare results of two queries that should be identical

I am modifying a sql server 2005 stored procedure slightly for performance, and I would like to quickly make sure the old stored proc and the new one return the exact same results (the columns are the same, I want to make sure the rows are the same).

Is there a simple way to do this in sql server 2005?

like image 854
Oxed Frederik Avatar asked Jun 13 '12 14:06

Oxed Frederik


People also ask

How do I compare the results of two SQL queries?

The solution to this is very simple. Run both queries using a UNION to combine the results! The UNION operator returns unique records. If the two results sets are identical the row count will remain the same as the original query.

How do you compare two queries performance?

Run the queries and compare logical reads for the various tables and execution times. @CombatCaptain You can also stack the comparing queries together in SSMS and press CTRL+M (include actual execution plan) and then F5 .

How do you check if two values are the same in SQL?

In SQL, you can use the = operator to test for equality in a query. In this example, the SELECT statement above would return all rows from the suppliers table where the supplier_name is equal to Microsoft.

Which operator in SQL Server is used to compare two result sets and return the data from the first result set that is not found in the second result set?

You can use EXCEPT or INTERSECT to compare more than two sets of queries.


1 Answers

you can use the except construct to match between the two queries.

select * from (select * from query1) as query1 except select * from (select * from query2) as query2 

EDIT:

Then reverse the query to find differences with query2 as the driver:

select * from (select * from query2) as query2 except select * from (select * from query1) as query1 
like image 93
jabs Avatar answered Sep 21 '22 18:09

jabs