SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Compare 2 datasets and output delta in one TSQL statement


Compare 2 datasets and output delta in one TSQL statement

Author
Message
Theo Ekelmans
Theo Ekelmans
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1638 Visits: 814
Comments posted to this topic are about the item Compare 2 datasets and output delta in one TSQL statement
macrocharlie
macrocharlie
Old Hand
Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)

Group: General Forum Members
Points: 371 Visits: 53
Where's the code?
tsceurman
tsceurman
SSC-Enthusiastic
SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)

Group: General Forum Members
Points: 152 Visits: 1011
Any idea on how well this scales, or its performance versus using a full join between the two tables with a WHERE clause where either side is NULL?
macrocharlie
macrocharlie
Old Hand
Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)

Group: General Forum Members
Points: 371 Visits: 53
OK, now the code is showing. Thanks.
Theo Ekelmans
Theo Ekelmans
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1638 Visits: 814
Hi,

As with any unbalanced compare, the performance varies.

Just compare this method with use your favorite compare and look closely at the execution plan

In my query close to 50% of the time is spent reading the 2 source tables using a clustered index scan, which is the most efficient way to read the ENTIRE table content.

The Merge join that does the actual compare work is only 19 % of the time

The rest: 33%, is spent storing the result.

If you find a more efficient way, i'd be happy to review your code!

Theo Smile
Mitesh Oswal
Mitesh Oswal
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1398 Visits: 653

--Compareandinsertdeltaintotable,inonesqlstatement
Insert into #cmpAB
([EmpNr_a],[EmpNr_b],
[Name_a],[Name_b],
[HireDT_a],[HireDT_b],
[FireDT_a],[FireDT_b]
)

SELECT a.[EmpNr],b.[EmpNr],a.[Name] , b.[Name],
a.[HireDT] , b.[HireDT] , a.[FireDT] , b.[FireDT]
FROM #tblA a INNER JOIN #tblB b
ON a.[EmpNr] = b.[EmpNr]
WHERE( a.[Name] <> b.[Name] OR a.[HireDT] <> b.[HireDT] OR a.[FireDT] <> b.[FireDT])

UNION
SELECT a.[EmpNr],b.[EmpNr],a.[Name] , b.[Name],
a.[HireDT] , b.[HireDT] , a.[FireDT] , b.[FireDT]
FROM #tblA a FULL OUTER JOIN #tblB b
ON a.[EmpNr] = b.[EmpNr]
where (a.[EmpNr] IS NULL OR b.[EmpNr] IS NULL)

--Thanks
Mitesh Oswal



Regards,
Mitesh OSwal
+918698619998
Iwas Bornready
Iwas Bornready
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29212 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search