SQL Server 2008 - HashBytes computed column

后端 未结 1 483
南方客
南方客 2021-01-01 17:35

I\'m using SQL Server 2008.

I have a NVARCHAR(MAX) column called Title and i want to add an unique index for it. Because the column is bigger than 9

相关标签:
1条回答
  • 2021-01-01 18:05

    The hashbytes column gets created as a VARBINARY(MAX) unless you do specifically tell it that 20 bytes are plenty:

    alter table dbo.Softs 
      add TitleHash AS CAST(hashbytes('SHA1', [Title]) AS VARBINARY(20)) PERSISTED
    

    Once you've done that, then you can create your index (unique or not) on that column:

    CREATE UNIQUE NONCLUSTERED INDEX [UIX_TitleHash] 
      ON [dbo].[Softs]([TitleHash] ASC)
    

    Now this should work just fine.

    0 讨论(0)
提交回复
热议问题