简体   繁体   English

在T-SQL中为字符串实现GetHashCode

[英]Implementing GetHashCode for string in T-SQL

I want to create a scalar function in T-SQL that would be analogous to those, say, in Java. 我想在T-SQL中创建一个类似于Java中的标量函数。

The standard implementation in imperative languages is: 命令式语言的标准实现是:

int hash = 0;
for (int i = 0; i < length; i++)
{
    hash = 31*hash + value[i];
}
return hash;

I'm not really that good in tsql to implement this, and from my POV writing imperative-style code in t-sql is something that should be avoided. 在tsql中,我实在不是很擅长实现这一点,从我的观点来看,应该避免在t-sql中编写命令式代码。 I guess this could be done using CTE? 我猜这可以使用CTE完成吗? Please =) 请=)

Also, can I make it always be positivie, ie when results exceed integrer max, then it flows over 0 rather than integer min? 另外,我可以使它始终为正值吗,即,当结果超过整数最大值时,它流过0而不是整数最小值? Let's assume that number of possible arguments (number of that special classes in my solution) is not really large. 假设可能的参数数量(解决方案中特殊类的数量)不是很大。 Let's say it will never exceed 1000, so I'm sure one can avoid collisions here even with uint. 假设它永远不会超过1000,所以我敢肯定即使在使用uint的情况下也可以避免碰撞。


PS: If someone's interesed what do I need this for, than I can explain, and probably you can suggest a better solution. PS:如果有人对此感兴趣,那么我需要什么,这超出了我的解释,也许您可​​以提出更好的解决方案。 I have a table with integer identity column and a varchar column 'TypeFullName' - thats a full name of a class in our C# solution. 我有一个带有integer标识列和varchar'TypeFullName' -这就是我们的C#解决方案中类的全名。

And I need to write a script that will manually set ID's as a function-dependence of TypeFullName (yes, turn on the SET IDENTITY INSERT option). 而且我需要编写一个脚本来手动将ID设置为TypeFullName的函数依赖性(是的,打开SET IDENTITY INSERT选项)。 So that I can compute ID, if I know the type name. 如果我知道类型名称,就可以计算ID。 I know that sounds like a system with bad design, and it probably is, but believe me, I just have to do this now) 我知道这听起来像是一个设计不良的系统,可能是这样,但是请相信我,我现在必须这样做)

Thank you! 谢谢!

Read my article on CheckSum versus Hashbytes. 阅读有关CheckSum与Hashbytes的文章。 ( http://craftydba.com/?p=3005 ) http://craftydba.com/?p=3005

They are two built in SQL Server functions that will generate you a hash key given a value. 它们是SQL Server的两个内置函数,它们将为您生成一个给定值的哈希键。 One is more unique than another. 一个比另一个更独特。

If you still have questions, just ask. 如果您还有问题,请问。

Sincerely 诚挚

John 约翰

www.craftydba.com www.craftydba.com

PS: PS:

You are losing precision when casting to a int or big int. 当转换为int或big int时,您将失去精度。 Just save it as a GUID (16 byte hex). 只需将其另存为GUID(16字节十六进制)。

在此处输入图片说明

I've found a solution in the internet and slightly updated it to limit output to positives: 我在互联网上找到了一种解决方案,并对其进行了稍微更新,以将输出限制为正值:

begin
declare @h bigint
set @h = 0
select @h = (@h*31 + ascii(substring(@str,X.pos,1)))%4294967296
   from (select top(len(@str)) 
             row_number() over (order by getdate()) as pos 
           from sys.all_objects) as X
if @h >= 2147483647 set @h = @h - 2147483647
return convert(int, @h)
end;

That select top from sys.all_objects is really hacky, but ((( at least it works. select top from sys.all_objects确实很hacky,但是(((至少它起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM