sql - 如何在 SQL Server 中的表类型上创建过滤索引?

标签 sql sql-server t-sql sql-server-2016

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.

最佳答案

从此Microsoft Article :

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/

相关文章:

sql - 在sql中使用多个pivot将行转换为列

sql - SQL 中的多个(级联)等于运算符

sql - 获取对象信息

sql-server - 如何获取字符串sql中某些字符之后的所有字符

sql - 在 SQL 中右对齐数字的最佳实践

Mysql确定是否更新找到行,即使没有改变

sql-server - 从 ADO.NET 类型化数据集创建 SQL 数据库

sql - 计算两个表之间的重叠值?

mysql - 生成更新查询

sql-server - DBMS 性能调优书籍推荐