我有一个相当大的nvarchar,我希望把它传递给HashBytes函数。我知道错误:
“字符串或二进制将被截断。无法将值NULL插入'colname‘、tbale’table‘列;列不允许空值。UPDATE失败。语句已终止。”
由于资源丰富,我发现这是由于HashBytes函数的最大限制为8000字节。进一步的搜索向我展示了一个“解决方案”,其中我的大型varchar将被分开并散列,然后与这个用户定义的函数组合在一起:
function [dbo].[udfLargeHashTable] (@algorithm nvarchar(4), @InputDataString varchar(MAX))
RETURNS varbinary(MAX)
AS
BEGIN
DECLARE
@Index int,
@InputDataLength int,
@ReturnSum varbinary(max),
@InputData varbinary(max)
SET @ReturnSum = 0
SET @Index = 1
SET @InputData = convert(binary,@InputDataString)
SET @InputDataLength = DATALENGTH(@InputData)
WHILE @Index <= @InputDataLength
BEGIN
SET @ReturnSum = @ReturnSum + HASHBYTES(@algorithm, SUBSTRING(@InputData, @Index, 8000))
SET @Index = @Index + 8000
END
RETURN @ReturnSum
END我称之为:
set @ReportDefinitionHash=convert(int,dbo.[udfLargeHashTable]('SHA1',@ReportDefinitionForLookup))其中@ReportDefinitionHash是int,@ReportDefinitionForLookup是varchar
通过一个简单的字符(如'test‘),在我的UDF中产生一个与对HashBytes的正常调用不同的int。
在这个问题上有什么建议吗?
发布于 2010-09-15 14:44:19
只需使用此函数(取自用用户定义的函数散列大型数据字符串):
create function dbo.fn_hashbytesMAX
( @string nvarchar(max)
, @Algo varchar(10)
)
returns varbinary(20)
as
/************************************************************
*
* Author: Brandon Galderisi
* Last modified: 15-SEP-2009 (by Denis)
* Purpose: uses the system function hashbytes as well
* as sys.fn_varbintohexstr to split an
* nvarchar(max) string and hash in 8000 byte
* chunks hashing each 8000 byte chunk,,
* getting the 40 byte output, streaming each
* 40 byte output into a string then hashing
* that string.
*
*************************************************************/
begin
declare @concat nvarchar(max)
,@NumHash int
,@HASH varbinary(20)
set @NumHash = ceiling((datalength(@string)/2)/(4000.0))
/* HashBytes only supports 8000 bytes so split the string if it is larger */
if @NumHash>1
begin
-- # * 4000 character strings
;with a as (select 1 as n union all select 1) -- 2
,b as (select 1 as n from a ,a a1) -- 4
,c as (select 1 as n from b ,b b1) -- 16
,d as (select 1 as n from c ,c c1) -- 256
,e as (select 1 as n from d ,d d1) -- 65,536
,f as (select 1 as n from e ,e e1) -- 4,294,967,296 = 17+ TRILLION characters
,factored as (select row_number() over (order by n) rn from f)
,factors as (select rn,(rn*4000)+1 factor from factored)
select @concat = cast((
select right(sys.fn_varbintohexstr
(
hashbytes(@Algo, substring(@string, factor - 4000, 4000))
)
, 40) + ''
from Factors
where rn <= @NumHash
for xml path('')
) as nvarchar(max))
set @HASH = dbo.fn_hashbytesMAX(@concat ,@Algo)
end
else
begin
set @HASH = convert(varbinary(20), hashbytes(@Algo, @string))
end
return @HASH
end结果如下:
select
hashbytes('sha1', N'test') --native function with nvarchar input
,hashbytes('sha1', 'test') --native function with varchar input
,dbo.fn_hashbytesMAX('test', 'sha1') --Galderisi's function which casts to nvarchar input
,dbo.fnGetHash('sha1', 'test') --your function输出:
0x87F8ED9157125FFC4DA9E06A7B8011AD80A53FE1
0xA94A8FE5CCB19BA61C4C0873D391E987982FBBD3
0x87F8ED9157125FFC4DA9E06A7B8011AD80A53FE1
0x00000000AE6DBA4E0F767D06A97038B0C24ED720662ED9F1发布于 2012-04-06 19:58:02
如果您不能创建一个函数,并且必须使用DB中已经存在的内容:
sys.fn_repl_hash_binary可以使用以下语法使其工作:
sys.fn_repl_hash_binary(cast('some really long string' as varbinary(max)))摘自:http://www.sqlnotes.info/2012/01/16/generate-md5-value-from-big-data/
发布于 2015-05-29 17:27:17
我接受了公认的答案,并对其进行了以下改进:
通过这些更改,这些函数现在可以在持久化计算列中使用,因为它们在创建时被标记为确定性。
CREATE FUNCTION dbo.fnHashBytesNVARCHARMAX
(
@Algorithm VARCHAR(10),
@Text NVARCHAR(MAX)
)
RETURNS VARBINARY(8000)
WITH SCHEMABINDING
AS
BEGIN
DECLARE @NumHash INT;
DECLARE @HASH VARBINARY(8000);
SET @NumHash = CEILING(DATALENGTH(@Text) / (8000.0));
/* HashBytes only supports 8000 bytes so split the string if it is larger */
WHILE @NumHash > 1
BEGIN
-- # * 4000 character strings
WITH a AS
(SELECT 1 AS n UNION ALL SELECT 1), -- 2
b AS
(SELECT 1 AS n FROM a, a a1), -- 4
c AS
(SELECT 1 AS n FROM b, b b1), -- 16
d AS
(SELECT 1 AS n FROM c, c c1), -- 256
e AS
(SELECT 1 AS n FROM d, d d1), -- 65,536
f AS
(SELECT 1 AS n FROM e, e e1), -- 4,294,967,296 = 17+ TRILLION characters
factored AS
(SELECT ROW_NUMBER() OVER (ORDER BY n) rn FROM f),
factors AS
(SELECT rn, (rn * 4000) + 1 factor FROM factored)
SELECT @Text = CAST
(
(
SELECT CONVERT(VARCHAR(MAX), HASHBYTES(@Algorithm, SUBSTRING(@Text, factor - 4000, 4000)), 1)
FROM factors
WHERE rn <= @NumHash
FOR XML PATH('')
) AS NVARCHAR(MAX)
);
SET @NumHash = CEILING(DATALENGTH(@Text) / (8000.0));
END;
SET @HASH = CONVERT(VARBINARY(8000), HASHBYTES(@Algorithm, @Text));
RETURN @HASH;
END;
CREATE FUNCTION dbo.fnHashBytesVARCHARMAX
(
@Algorithm VARCHAR(10),
@Text VARCHAR(MAX)
)
RETURNS VARBINARY(8000)
WITH SCHEMABINDING
AS
BEGIN
DECLARE @NumHash INT;
DECLARE @HASH VARBINARY(8000);
SET @NumHash = CEILING(DATALENGTH(@Text) / (8000.0));
/* HashBytes only supports 8000 bytes so split the string if it is larger */
WHILE @NumHash > 1
BEGIN
-- # * 4000 character strings
WITH a AS
(SELECT 1 AS n UNION ALL SELECT 1), -- 2
b AS
(SELECT 1 AS n FROM a, a a1), -- 4
c AS
(SELECT 1 AS n FROM b, b b1), -- 16
d AS
(SELECT 1 AS n FROM c, c c1), -- 256
e AS
(SELECT 1 AS n FROM d, d d1), -- 65,536
f AS
(SELECT 1 AS n FROM e, e e1), -- 4,294,967,296 = 17+ TRILLION characters
factored AS
(SELECT ROW_NUMBER() OVER (ORDER BY n) rn FROM f),
factors AS
(SELECT rn, (rn * 8000) + 1 factor FROM factored)
SELECT @Text = CAST
(
(
SELECT CONVERT(VARCHAR(MAX), HASHBYTES(@Algorithm, SUBSTRING(@Text, factor - 8000, 8000)), 1)
FROM factors
WHERE rn <= @NumHash
FOR XML PATH('')
) AS NVARCHAR(MAX)
);
SET @NumHash = CEILING(DATALENGTH(@Text) / (8000.0));
END;
SET @HASH = CONVERT(VARBINARY(8000), HASHBYTES(@Algorithm, @Text));
RETURN @HASH;
END;https://stackoverflow.com/questions/3717977
复制相似问题