校验和功能取决于Unicode?

时间:2011-09-22 08:17:06

标签: sql-server

如果它在 unicode 中,那么结果:(注意 N

select  CHECKSUM(N'2Volvo Director 20') ---341465450
select  CHECKSUM(N'3Volvo Director 30') ---341453853
select  CHECKSUM(N'4Volvo Director 40') ---341455363

但如果是常规:

select  CHECKSUM('2Volvo Director 20') ---1757834048
select  CHECKSUM('3Volvo Director 30') ---1757834048
select  CHECKSUM('4Volvo Director 40') ---1757834048

你能否解释我为什么在第一种情况下 - 它给了我不同的东西,在第二种情况下它给了我相同的东西?

有一篇关于它的主要文章说:

但是CHECKSUM()函数会评估类型并比较两个字符串,如果它们相等,则只返回相同的值。

1 个答案:

答案 0 :(得分:1)

这似乎与整理相关。

DECLARE @T TABLE
(
SQL_Latin1_General_CP1255_CI_AS varchar(100) COLLATE SQL_Latin1_General_CP1255_CI_AS,
Latin1_General_CI_AS varchar(100) COLLATE Latin1_General_CI_AS
)
INSERT INTO @T
SELECT '2Volvo Director 20','2Volvo Director 20' UNION ALL 
SELECT '3Volvo Director 30','3Volvo Director 30' UNION ALL 
SELECT '4Volvo Director 40','4Volvo Director 40' UNION ALL 
SELECT '5Volvo Director 50','5Volvo Director 50' UNION ALL 
SELECT '6Volvo Director 60','6Volvo Director 60'

SELECT 
       CHECKSUM(SQL_Latin1_General_CP1255_CI_AS) AS SQL_Latin1_General_CP1255_CI_AS,
       CHECKSUM(Latin1_General_CI_AS) AS Latin1_General_CI_AS
FROM @T

返回

SQL_Latin1_General_CP1255_CI_A Latin1_General_CI_AS
------------------------------ --------------------
-1757834048                    -341465450
-1757834048                    -341453853
-1757834048                    -341455363
-1757834048                    -341442609
-1757834048                    -341448488

CHECKSUM documentedHashBytes更容易发生碰撞。我不确定具体为什么CP排序规则对这些输入有这种行为。