2012-05-07 40 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派生列 –