计算列作为具有格式化日期时间的全文索引

时间:2017-04-05 08:32:48

标签: sql tsql full-text-indexing deterministic

我有下表:

[dbo].[TableName]
(
[created] datetime not null,
[modified] datetime null,
[computedValue] as 
(
    format([created], 'yyy-MM-dd MM/dd/yyy') +
    case when ([modified] is not null)
    then 
            (format([modified], 'yyyy-MM-dd MM/dd/yyyy')) 
    end
)

当我尝试在此表上更改全文索引时会出现问题。

ALTER FULLTEXT INDEX ON [dbo].[TableName] ADD ([computedValue] LANGUAGE 1033);

作为回应,我收到以下错误:

Computed column 'computedValue' cannot be used for full-text search because it is nondeterministic or imprecise nonpersisted computed column.

根据MSDN: https://msdn.microsoft.com/en-us/library/ms181984(v=sql.110).aspx

“所有内置字符串函数都是确定性的。” 所以我在绞尽脑汁为什么说这个专栏不确定。

1 个答案:

答案 0 :(得分:1)

你能试试吗? (它在MSSQL 2008上对我有用)(FORMAT()似乎是不确定的(另见[link] Can I make SQL Server FORMAT deterministic?

    CREATE TABLE TableName
    (ID INT NOT NULL,
    [created] datetime not null,
    [modified] datetime null,
    [computedValue] as (
        CONVERT(varchar(10), [created], 121) +' '  + CONVERT(varchar(10), [created], 110)
        + CASE WHEN ([modified] IS NOT NULL)
        THEN CONVERT(varchar(10), [modified], 121) +' '  + CONVERT(varchar(10), [modified], 110) 
        END 
    ) 
    )
    ;
    ALTER TABLE TABLENAME ADD CONSTRAINT TABLENAME_PK PRIMARY KEY (ID);
   GO   
    CREATE FULLTEXT CATALOG CATALOG1;  
    GO  
    CREATE FULLTEXT INDEX ON [TableName]([computedValue]  LANGUAGE 1033 ) KEY INDEX TABLENAME_PK ON CATALOG1;