我有一个 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.
转换为 smalldatetime
为 non-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/