EXEMPLO_1212310
Database
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
GetHashCode
Parameters
Name
Type
Mode
@input
varchar
IN
@hash
int
INOUT
Definition
CREATE PROCEDURE [dbo].[GetHashCode] @input tAppName, @hash int OUTPUT AS /* This sproc is based on this C# hash function: int GetHashCode(string s) { int hash = 5381; int len = s.Length; for (int i = 0; i < len; i++) { int c = Convert.ToInt32(s[i]); hash = ((hash << 5) + hash) ^ c; } return hash; } However, SQL 7 doesn't provide a 32-bit integer type that allows rollover of bits, we have to divide our 32bit integer into the upper and lower 16 bits to do our calculation. */ DECLARE @hi_16bit int DECLARE @lo_16bit int DECLARE @hi_t int DECLARE @lo_t int DECLARE @len int DECLARE @i int DECLARE @c int DECLARE @carry int SET @hi_16bit = 0 SET @lo_16bit = 5381 SET @len = DATALENGTH(@input) SET @i = 1 WHILE (@i <= @len) BEGIN SET @c = ASCII(SUBSTRING(@input, @i, 1)) /* Formula: hash = ((hash << 5) + hash) ^ c */ /* hash << 5 */ SET @hi_t = @hi_16bit * 32 /* high 16bits << 5 */ SET @hi_t = @hi_t & 0xFFFF /* zero out overflow */ SET @lo_t = @lo_16bit * 32 /* low 16bits << 5 */ SET @carry = @lo_16bit & 0x1F0000 /* move low 16bits carryover to hi 16bits */ SET @carry = @carry / 0x10000 /* >> 16 */ SET @hi_t = @hi_t + @carry SET @hi_t = @hi_t & 0xFFFF /* zero out overflow */ /* + hash */ SET @lo_16bit = @lo_16bit + @lo_t SET @hi_16bit = @hi_16bit + @hi_t + (@lo_16bit / 0x10000) /* delay clearing the overflow */ /* ^c */ SET @lo_16bit = @lo_16bit ^ @c /* Now clear the overflow bits */ SET @hi_16bit = @hi_16bit & 0xFFFF SET @lo_16bit = @lo_16bit & 0xFFFF SET @i = @i + 1 END /* Do a sign extension of the hi-16bit if needed */ IF (@hi_16bit & 0x8000 <> 0) SET @hi_16bit = 0xFFFF0000 | @hi_16bit /* Merge hi and lo 16bit back together */ SET @hi_16bit = @hi_16bit * 0x10000 /* << 16 */ SET @hash = @hi_16bit | @lo_16bit RETURN 0