tsql - Why does CHECKSUM_AGG() return same values for totally different input values? -



tsql - Why does CHECKSUM_AGG() return same values for totally different input values? -

i don't how checksum_agg() works, although understood somehow built using xors. explains why returns 0 when pass equal integers. why same aggregated checksum in next sql, input values unique?

declare @test1 table (chksum int) insert @test1 values (2147473855), (2147473343) select checksum_agg(chksum) @test1 -- declare @test2 table (chksum int) insert @test2 values (2147474831), (2147472271) select checksum_agg(chksum) @test2

an explanation much appreciated. thanks!

there known issues sql server checksum , checksum_agg implementations: checksum weakness explained

use hashbytes instead: using hashbytes compare columns

from microsoft: if 1 of values in look list changes, checksum of list changes. however, there little chance checksum not change. reason, not recommend using checksum observe whether values have changed, unless application can tolerate missing change. consider using hashbytes instead. when md5 hash algorithm specified, probability of hashbytes returning same result 2 different inputs much lower of checksum.

you may not utilize hashbytes across rows straight - there 1 workaround here.

here comparing smaller numbers, using hasbytes workaround:

declare @test1 table (chksum int) declare @test2 table (chksum int) insert @test1 values (50), (3), (26) insert @test2 values (45), (0), (6) select [values] = '50, 3, 26', [checksum] = checksum_agg(chksum), -- hashbytes limited 8000 bytes [hashbytes] = hashbytes('md5',convert(varbinary(max),(select * @test1 xml auto))) @test1 union select [values] = '45, 0, 6', [checksum] = checksum_agg(chksum), -- hashbytes limited 8000 bytes [hashbytes] = hashbytes('md5',convert(varbinary(max),(select * @test2 xml auto))) @test2

tsql

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -