Creating index on computed column of type nvarchar raises following error:
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.
What does imprecise column mean?
UPDATE. The definition is following:
alter table dbo.MyTable
add [MyColumn] as dbo.MyDeterministicClrFunction(MyOtherColumn)
go
create index MyIndex on dbo.MyTable(MyColumn)
go
UPDATE2. The MyDeterministicClrFunction is defined as following:
[SqlFunction(IsDeterministic = true)]
public static SqlString MyDeterministicClrFunction(SqlString input)
{
return input;
}
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.
Persist the column and I suspect it will work.
From the 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).
Have you tried:
[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
Sounds like the error message is misleading because CLR computed columns have to be persisted anyway (to be indexed).
来源:https://stackoverflow.com/questions/10484036/what-is-imprecise-column-in-sql-server