如何返回重复的varbinary列

时间:2013-05-25 16:33:56

标签: sql-server sql-server-2008

我有一个SQL 2008表,它列出了一堆文件的一些元数据,如下所示:

ID bigint FileName nvarchar(255) FileHash varbinary(512)

FileHash是FileName列中文件路径的SHA-512哈希。

我可以运行SELECT并返回具有相同FileHash的FileNames吗?

1 个答案:

答案 0 :(得分:1)

declare @Nezbit as Table ( Id BigInt Identity, FileName NVarChar(255), FileHash VarBinary(512) );

insert into @Nezbit ( FileName, FileHash ) values
  ( 'Bob', 0x1234 ),
  ( 'Carol', 0x5678 ),
  ( 'Ted', 0x9abc ),
  ( 'Alice', 0xdef0 ),
  ( 'Robert', 0x1234 ),
  ( 'Lydia', 0xdef0 );

select FileName, FileHash
  from @Nezbit as N
  where exists ( select 42 from @Nezbit where FileHash = N.FileHash and Id <> N.Id )
  order by FileHash, FileName;