Last active
January 27, 2025 18:52
-
-
Save brunotdantas/022157695a55c52e4d7eaa61ccf8230b to your computer and use it in GitHub Desktop.
Hash SQL SERVER table row
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- https://stackoverflow.com/questions/11186183/hash-a-sql-row/54789730#54789730 | |
drop table if exists #table1; | |
create table #table1 ( | |
campo varchar(10) | |
,campo1 varchar(10) | |
) | |
drop table if exists #table2; | |
create table #table2 ( | |
campo varchar(10) | |
,campo1 varchar(10) | |
) | |
insert into #table1 values ('bruno',1) | |
insert into #table1 values ('bruno',2) | |
insert into #table2 values ('bruno',1) -- This combination value belongs to #table2 and #table1 | |
insert into #table2 values ('bruna',2) -- This combination value doesn't belong to #table1 | |
select *,HASHBYTES('SHA1', (select z.* FOR XML RAW)) as hash | |
into #compare1 | |
from #table1 z | |
select *,HASHBYTES('SHA1', (select k.* FOR XML RAW)) as hash | |
into #compare2 | |
from #table2 k | |
-- pull distinct values from both tables | |
drop table if exists #TMP; | |
select A.*,'table A' TABLE_NAME INTO #TMP from #compare1 a | |
full outer join #compare2 b on a.hash = b.hash | |
where ( a.hash is null or b.hash is null ) | |
INSERT INTO #TMP | |
select B.*,'table B' from #compare1 a | |
full outer join #compare2 b on a.hash = b.hash | |
where ( a.hash is null or b.hash is null ) | |
DELETE FROM #TMP WHERE campo is null -- IGNORE NULL VALUES | |
select * from #TMP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment