有没有在SQL Server 2012中的任何方式来生成一组行和列的哈希?
我想生成一个散列值,其存储在父记录。 该当一个更新进来,我将与父记录哈希比较传入散,我就知道数据是否已经改变。
因此,像这样就好了:
SELECT GENERATEHASH(CONCATENATE(Name, Description, AnotherColumn))
FROM MyChildTable WHERE ParentId = 2 -- subset of data belong to parent record 2
“CONCATENATE”将是一个聚合函数,这不仅CONCAT列,而且,在结果集内的行。 MAX一样,但返回的一切作为一个字符串连接。
希望这有助于你明白我的意思呢!
我试图解决最根本的问题是,我的客户的系统中执行的大量分层数据的进口。 如果我可以通过使用哈希避免处理,那么我认为这将节省大量的时间。 目前,具有处理重复的数据在SP的运行速度300%。
非常感谢
您可以使用CHECKSUM_AGG骨料。 它被用于此目的的。
select HashBytes('md5',convert(varbinary(max),(SELECT * FROM MyChildTable WHERE ParentId = 2 FOR XML AUTO)))
但HASHBYTES仅限于8000个字节......你可以做一个函数来获得德MD5具有每8000个字节....
对于单行哈希:
select HASHBYTES('md5', Name + Description + AnotherColumn)
FROM MyChildTable WHERE ParentId = 2
为表校验:
select sum(checksum(Name + Description + AnotherColumn)*1.0)
FROM MyChildTable WHERE ParentId = 2
另一种方法:
-- compute a single hash value for all rows of a table
begin
set nocount on;
-- init hash variable
declare @tblhash varchar(40);
set @tblhash = 'start';
-- compute a single hash value
select @tblhash = sys.fn_varbintohexsubstring(0, hashbytes('sha1',(convert(varbinary(max),@tblhash+
(select sys.fn_varbintohexsubstring(0,hashbytes('sha1',(convert(varbinary(max),
-- replace 'select *' if you want only specific columns to be included in the hash calculation
-- [target table] is the name of the table to calc the hash from
-- [row_id] is the primary key column within the target table
-- modify those in the next lines to suit your needs:
(select * from [target_table] obj2 where obj2.[row_id]=obj1.[row_id] for xml raw)
))),1,0))
))),1,0)
from [target_table] obj1;
set nocount off;
-- return result
select @tblhash as hashvalue;
end;