2012-05-07 12 views
8

nvarchar类型的计算列创建索引提出了以下错误:什么是SQL Server中的不精确列?

Cannot create index or statistics 'MyIndex' on table 'MyTable' because the computed column 'MyColumn' is imprecise and not persisted. Consider removing column from index or statistics key or marking computed column persisted.

是什么不精确列是什么意思?

UPDATE。定义如下:

alter table dbo.MyTable 
    add [MyColumn] as dbo.MyDeterministicClrFunction(MyOtherColumn) 
go 
create index MyIndex on dbo.MyTable(MyColumn) 
go 

UPDATE2。该MyDeterministicClrFunction定义如下:

[SqlFunction(IsDeterministic = true)] 
public static SqlString MyDeterministicClrFunction(SqlString input) 
{ 
    return input; 
} 
+0

你想用什么公式计算列值? – Yuck

+0

它是'n​​varchar(xx)'还是'nvarchar(max)'? – JNK

+0

'MyOtherColumn'其他列是'nvarchar(50)'。 –

回答

9

Per MSDN, CLR Function columns must be persisted to be indexed:

Any computed column that contains a common language runtime (CLR) expression must be deterministic and marked PERSISTED before the column can be indexed. CLR user-defined type expressions are allowed in computed column definitions. Computed columns whose type is a CLR user-defined type can be indexed as long as the type is comparable. For more information, see CLR User-Defined Types.

坚持列,我怀疑它会工作。

+0

所以这个消息是错误的,说这是不准确的? –

+0

@TN。我认为错误信息可能不是特定于这种情况,而是与此错误相关的更一般的消息。 – JNK

+0

好吧,我看到了...... :) –

6

SQL server documentation

Any float or real expression is considered imprecise and cannot be a key of an index; a float or real expression can be used in an indexed view but not as a key. This is true also for computed columns. Any function, expression, or user-defined function is considered imprecise if it contains any float or real expressions. This includes logical ones (comparisons).

+2

正如你可以看到我的列有'nvarchar'类型。 –

+0

@TN - 可能是因为它没有指定生成的varchar的大小? –

+0

或者它可能是因为它不被持久 – JNK

1

你试过:

[SqlFunction(IsDeterministic=true, IsPrecise=true)] 

http://msdn.microsoft.com/en-us/library/orm-9780596101404-02-12.aspx

http://msdn.microsoft.com/en-us/library/ms189292.aspx

听起来就像是错误消息误导,因为CLR计算列必须坚持反正(被编入索引)。

+0

+1 Thx,但它给出了稍微好一点的错误信息:'不能在表'MyTable'上创建索引或统计'MyIndex',因为SQL Server无法验证'MyColumn'键列是否精确和确定性的。考虑从索引或统计关键字中移除列,将计算列标记为持久化,或者使用key中的非CLR派生列 –