Monday, May 30, 2011

MS Sqlserver unique index with null values

Monday, May 30, 2011 Posted by Andre Broers , , , 1 comment
I needed a unique constraint where null values are allowed. This is the way in sql server 2008+

Strange syntax but it works. ;-)
USE [db]
GO
/****** Object:  Index [IX_person]    Script Date: 05/30/2011 14:35:14 ******/
CREATE UNIQUE NONCLUSTERED INDEX [IX_person] ON [dbo].[person]
(
  [pername] ASC
)
WHERE ([pername] IS NOT NULL)
GO

1 comment: