sql - 不能在索引或统计中使用或用作分区键

标签 sql

我有一个 Varchar 列,其日期类似于 01/02/2020 以及“N?A” 我想使用以下表达式创建一个名为 DateValue 的计算列:

(case when isdate([Value])=(1) then CONVERT(smalldatetime, [Value]) else null  end)

然后我想在 DateValue 上放置一个非聚集索引,但是我得到了

Column 'DateValue' in table 'dbo.tblTrkCustomDate' cannot be used in an index or statistics or as a partition key because it is non-deterministic.

对此有什么想法吗?

最佳答案

这是由于创建索引时的一个细节造成的。要在索引中使用,表达式必须是确定性的。作为documentation描述:

Expressions are deterministic if they always return the same result for a specified set of inputs. The IsDeterministic property of the COLUMNPROPERTY function reports whether a computed_column_expression is deterministic.

转换为 smalldatetimenon-deterministic under these conditions :

Source or target type is datetime or smalldatetime, the other source or target type is a character string, and a nondeterministic style is specified. To be deterministic, the style parameter must be a constant. Additionally, styles less than or equal to 100 are nondeterministic, except for styles 20 and 21. Styles greater than 100 are deterministic, except for styles 106, 107, 109 and 113.

因此,如果您可以选择一种确定性样式,那么这应该可行。

关于sql - 不能在索引或统计中使用或用作分区键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61821804/

相关文章:

sql - 使用 SQL 获取平均值

sql - 从现有表插入?

mysql - 我正在使用 2 个查询,有什么方法可以使它成为单个查询

mysql - Nedd 帮助理解为什么只有带有子查询的 JOIN 返回正确的值

sql - 在SQL中不更新行

php - 使用 PHP 将 SQL Join 语句连接到 HTML 表中的单个字段

mysql - 如何连接 5 个不同表中的记录?

sql - 为什么 Dapper 即使没有异常(exception)也不更新数据到 MS Access?

sql - 如何仅使用一个 sql 脚本 db2 将数据同时插入到两个表中?

使用 VB.NET 的 SQL Server 数据库新架构