CREATE TYPE [dbo].[MyTableType] AS TABLE
(
Id INT NULL INDEX ix UNIQUE WHERE Id IS NOT NULL /*Unique ignoring nulls*/
, Sort INT NOT NULL
, [Name] NVARCHAR(MAX) NULL
)
SQL Server 2016 中执行此操作的新方法是什么?我一直遇到以下错误
Incorrect syntax near 'INDEX'. If this is intended as a part of a table hint, a WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax.
最佳答案
A nonclustered index cannot be created on a user-defined table type unless the index is the result of creating a PRIMARY KEY or UNIQUE constraint on the user-defined table type. (SQL Server enforces any UNIQUE or PRIMARY KEY constraint by using an index.)
我认为为表类型创建唯一索引的唯一方法是:
CREATE TYPE [dbo].[MyTableType] AS TABLE
(
Id INT NULL UNIQUE
, Sort INT NOT NULL
, [Name] NVARCHAR(MAX) NULL
)
我认为您不能添加类似于以下内容的“忽略 NULLS”选项:
CREATE UNIQUE NONCLUSTERED INDEX idx_yourcolumn_notnull
ON YourTable(yourcolumn)
WHERE yourcolumn IS NOT NULL;
关于sql - 如何在 SQL Server 中的表类型上创建过滤索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48846850/