Skip to content

Instantly share code, notes, and snippets.

@spaghettidba
Last active March 7, 2018 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spaghettidba/6e279d241263056d618619f6cd8976e9 to your computer and use it in GitHub Desktop.
Save spaghettidba/6e279d241263056d618619f6cd8976e9 to your computer and use it in GitHub Desktop.
verify_script.sql #blog
-- http://spaghettidba.com/2012/03/15/how-to-eat-a-sql-elephant/
-- =============================================
-- Author: Gianluca Sartori - spaghettidba
-- Create date: 2012-03-14
-- Description: Runs two T-SQL statements and
-- compares the results
-- =============================================
-- Drop temporary tables
IF OBJECT_ID('tempdb..#original') IS NOT NULL
DROP TABLE #original;
IF OBJECT_ID('tempdb..#rewritten') IS NOT NULL
DROP TABLE #rewritten;
-- Store the results of the original
-- query into a temporary table
WITH original AS (
<original, text, >
)
SELECT *
INTO #original
FROM original;
-- Add a sort column
ALTER TABLE #original ADD [______sortcolumn] int identity(1,1);
-- Store the results of the rewritten
-- query into a temporary table
WITH rewritten AS (
<rewritten, text, >
)
SELECT *
INTO #rewritten
FROM rewritten;
-- Add a sort column
ALTER TABLE #rewritten ADD [______sortcolumn] int identity(1,1);
-- Compare the results
SELECT 'original' AS source, *
FROM (
SELECT *
FROM #original
EXCEPT
SELECT *
FROM #rewritten
) AS A
UNION ALL
SELECT 'rewritten' AS source, *
FROM (
SELECT *
FROM #rewritten
EXCEPT
SELECT *
FROM #original
) AS B;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment