我试图计算在SQL Server整个表校验或哈希2008年我遇到的问题是,该表包含XML列数据类型,无法通过校验使用,必须转换为nvarchar第一。 所以,我需要把它分解为两个问题:
- 计算校验和一排,模式是运行时之前未知。
- 计算所有行的校验,以获得完整的表校验。
我试图计算在SQL Server整个表校验或哈希2008年我遇到的问题是,该表包含XML列数据类型,无法通过校验使用,必须转换为nvarchar第一。 所以,我需要把它分解为两个问题:
您可以使用CHECKSUM_AGG 。 只需要一个参数,所以你可以做CHECKSUM_AGG(CHECKSUM(*))
-但是这并不能为你的XML数据类型的工作,所以你必须诉诸于动态SQL。
你可以动态生成列列表INFORMATION_SCHEMA.COLUMNS
然后插入INT到模板:
SELECT @column_list = COALESCE(@column_list + ', ', '')
+ /* Put your casting here from XML, text, etc columns */ QUOTENAME(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @table_name
AND TABLE_SCHEMA = @schema_name
DECLARE @template AS varchar(MAX)
SET @template = 'SELECT CHECKSUM_AGG(CHECKSUM({@column_list})) FROM {@schema_name}.{@table_name}'
DECLARE @sql AS varchar(MAX)
SET @sql = REPLACE(REPLACE(REPLACE(
'{@column_list}', @column_list),
'{@schema_name}', @schema_name),
'{@table_name}', @table_name)
EXEC ( @sql )
我修改了脚本生成的数据库中的所有相关表的查询。
USE myDatabase
GO
DECLARE @table_name sysname
DECLARE @schema_name sysname
SET @schema_name = 'dbo'
DECLARE myCursor cursor
FOR SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES T
WHERE T.TABLE_SCHEMA = @schema_name
AND T.TABLE_TYPE = 'BASE TABLE'
AND T.TABLE_NAME NOT LIKE 'MSmerge%'
AND T.TABLE_NAME NOT LIKE 'sysmerge%'
AND T.TABLE_NAME NOT LIKE 'tmp%'
ORDER BY T.TABLE_NAME
OPEN myCursor
FETCH NEXT
FROM myCursor
INTO @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @column_list nvarchar(MAX)
SET @column_list=''
SELECT @column_list = @column_list + CASE WHEN DATA_TYPE IN ('xml','text','ntext','image sql_variant') THEN 'CONVERT(nvarchar(MAX),'
ELSE ''
END
+ QUOTENAME(COLUMN_NAME)
+ CASE WHEN DATA_TYPE IN ('xml','text','ntext','image sql_variant') THEN ' /* ' + DATA_TYPE + ' */)'
ELSE ''
END + ', '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @Table_name
ORDER BY ORDINAL_POSITION
SET @column_list = LEFT(@column_list, LEN(@column_list)-1) -- remove trailing comma
DECLARE @sql AS nvarchar(MAX)
SET @sql = 'SELECT ''' + QUOTENAME(@schema_name) + '.' + QUOTENAME(@table_name) + ''' table_name,
CHECKSUM_AGG(CHECKSUM(' + @column_list + ')) CHECKSUM
FROM ' + QUOTENAME(@schema_name) + '.' + QUOTENAME(@Table_name) + ' WITH (NOLOCK)'
PRINT @sql
FETCH NEXT
FROM myCursor
INTO @table_name
IF @@FETCH_STATUS = 0
PRINT 'UNION ALL'
END
CLOSE myCursor
DEALLOCATE myCursor
GO
// SQL和C#镜乌克兰的快速散列和// HASH_ZKCRC64 /// -------------------------------- -------------------------------------------------- ----------------------------私人的Int64 HASH_ZKCRC64(字节[]数据){Int64的结果= 0x5555555555555555; 如果(数据== NULL || Data.Length <= 0)返回0; INT SizeGlobalBufer = 8000; INT奥斯特= Data.Length%SizeGlobalBufer; INT LeftLimit =(Data.Length / SizeGlobalBufer)* SizeGlobalBufer;
for (int i = 0; i < LeftLimit; i += 64)
{
Result = Result
^ BitConverter.ToInt64(Data, i)
^ BitConverter.ToInt64(Data, i + 8)
^ BitConverter.ToInt64(Data, i + 16)
^ BitConverter.ToInt64(Data, i + 24)
^ BitConverter.ToInt64(Data, i + 32)
^ BitConverter.ToInt64(Data, i + 40)
^ BitConverter.ToInt64(Data, i + 48)
^ BitConverter.ToInt64(Data, i + 56);
if ((Result & 0x0000000000000080) != 0)
Result = Result ^ BitConverter.ToInt64(Data, i + 28);
}
if (Ost > 0)
{
byte[] Bufer = new byte[SizeGlobalBufer];
Array.Copy(Data, LeftLimit, Bufer, 0, Ost);
for (int i = 0; i < SizeGlobalBufer; i += 64)
{
Result = Result
^ BitConverter.ToInt64(Bufer, i)
^ BitConverter.ToInt64(Bufer, i + 8)
^ BitConverter.ToInt64(Bufer, i + 16)
^ BitConverter.ToInt64(Bufer, i + 24)
^ BitConverter.ToInt64(Bufer, i + 32)
^ BitConverter.ToInt64(Bufer, i + 40)
^ BitConverter.ToInt64(Bufer, i + 48)
^ BitConverter.ToInt64(Bufer, i + 56);
if ((Result & 0x0000000000000080)!=0)
Result = Result ^ BitConverter.ToInt64(Bufer, i + 28);
}
}
byte[] MiniBufer = BitConverter.GetBytes(Result);
Array.Reverse(MiniBufer);
return BitConverter.ToInt64(MiniBufer, 0);
#region SQL_FUNCTION
/* CREATE FUNCTION [dbo].[HASH_ZKCRC64] (@data as varbinary(MAX)) Returns bigint
AS
BEGIN
Declare @I64 as bigint Set @I64=0x5555555555555555
Declare @Bufer as binary(8000)
Declare @i as int Set @i=1
Declare @j as int
Declare @Len as int Set @Len=Len(@data)
if ((@data is null) Or (@Len<=0)) Return 0
While @i<=@Len
Begin
Set @Bufer=Substring(@data,@i,8000)
Set @j=1
While @j<=8000
Begin
Set @I64=@I64
^ CAST(Substring(@Bufer,@j, 8) as bigint)
^ CAST(Substring(@Bufer,@j+8, 8) as bigint)
^ CAST(Substring(@Bufer,@j+16,8) as bigint)
^ CAST(Substring(@Bufer,@j+24,8) as bigint)
^ CAST(Substring(@Bufer,@j+32,8) as bigint)
^ CAST(Substring(@Bufer,@j+40,8) as bigint)
^ CAST(Substring(@Bufer,@j+48,8) as bigint)
^ CAST(Substring(@Bufer,@j+56,8) as bigint)
if @I64<0 Set @I64=@I64 ^ CAST(Substring(@Bufer,@j+28,8) as bigint)
Set @j=@j+64
End;
Set @i=@i+8000
End
Return @I64
END
*/
#endregion
}