I'll assume you have a case-insensitive collation for your instance of SQL Server. How about something like this:
CREATE TABLE #TABLE1 (
ID INT NOT NULL PRIMARY KEY CLUSTERED,
COL1 varchar(6),
COL2 varchar(6),
COL3 varchar(6),
COL4 varchar(6),
COL5 varchar(6)
)
CREATE TABLE #TABLE2 (
ID INT NOT NULL PRIMARY KEY CLUSTERED,
COL1 varchar(6),
COL2 varchar(6),
COL3 varchar(6),
COL4 varchar(6),
COL5 varchar(6)
)
INSERT INTO #TABLE1 VALUES (1, 'This', 'That', 'or', 'yes', 'me')
INSERT INTO #TABLE2 VALUES (1, 'This', 'That', 'not', 'yes', 'you')
;WITH T1_ROWS AS (
SELECT ID, FIELD, COLVALUE
FROM #TABLE1
UNPIVOT (COLVALUE FOR FIELD IN (COL1, COL2, COL3, COL4, COL5)) AS UPVT
),
T2_ROWS AS (
SELECT ID, FIELD, COLVALUE
FROM #TABLE2
UNPIVOT (COLVALUE FOR FIELD IN (COL1, COL2, COL3, COL4, COL5)) AS UPVT
)
SELECT T1.ID, T1.FIELD, T1.COLVALUE AS T1_VALUE, T2.COLVALUE AS T2_VALUE
FROM T1_ROWS AS T1
INNER JOIN T2_ROWS AS T2
ON T1.ID = T2.ID
AND T1.FIELD = T2.FIELD
WHERE T1.COLVALUE <> T2.COLVALUE
DROP TABLE #TABLE1
DROP TABLE #TABLE2
A significant caveat is in order here. You''d have to realize that just having the specific columns that differ in one case could be completely different the next time, so if you were to use this data to feed a web app or a report, you could have column name problems, so my approach was slightly different for precisely this reason.
While there is a way to pivot my results back to what you're looking for, you would trouble the moment there's more than one unique ID value, as then you'd have to have columns for ALL 5 potential mismatches, as yoiu can't have differing column names on each output record.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)