Update:
Unfortunately, the answer provided here did not give me what I wanted - My mistake for accepting too quickly without really doing a deep enough check.
I have reported the question here.
Suppose I have the following two tables in my SQL Server (2012) DB:
Tbl1:
ID: Col1: Col2: Col3:
1 Val1 Val2 Val3
2 <NULL> Val2 <NULL>
3 Val1 <NULL> Val3
4 Val1 <NULL> Val3
Tbl2:
ID: Col1: Col2: Col3:
1 Val1 Val2 Val3
2 <NULL> Val2 <NULL>
3 <NULL> <NULL> Val3
5 <NULL> <NULL> Val3
And, at this point, all I want to see is:
- Any rows that are in one table but not the other (based on
ID
pk) - If the
ID
is in both tables, then are the same columns populated (not caring specifically about the values yet).
I'm just trying to come up with a SQL to just let me know which ID
s have discrepancies.
My ideal output would look as follows:
Tbl1_ID: Tbl2_Id: Discrepancy:
1 1 0
2 2 0
3 3 1
4 <NULL> 1
<NULL> 5 1
My testing SQL so far is this:
DECLARE
@Tbl1 TABLE (ID INT, Col1 VARCHAR(10), Col2 VARCHAR(10), Col3 VARCHAR(10))
;
DECLARE
@Tbl2 TABLE (ID INT, Col1 VARCHAR(10), Col2 VARCHAR(10), Col3 VARCHAR(10))
;
INSERT INTO @Tbl1 (ID, Col1, Col2, Col3)
VALUES
(1, 'Val1', 'Val2', 'Val3')
,(2, NULL , 'Val2', NULL)
,(3, 'Val1', NULL, 'Val3')
,(4, 'Val1', NULL, 'Val3')
;
INSERT INTO @Tbl2 (ID, Col1, Col2, Col3)
VALUES
(1, 'Val1', 'Val2', 'Val3')
,(2, NULL , 'Val2', NULL)
,(3, NULL, NULL, 'Val3')
,(5, NULL, NULL, 'Val3')
;
SELECT
[@Tbl1].ID AS Tbl1_ID
,[@Tbl2].ID AS Tbl2_ID
, -- Some kind of comparison to let me know if all columns are populated the same
AS Discrepancy
FROM
@Tbl1
FULL JOIN @Tbl2
ON [@Tbl1].ID = [@Tbl2].ID
Is there some good way to accomplish this?
Thanks!!