Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

sql - Why is my CASE expression non-deterministic?

I am trying to create a persisted computed column using CASE expression:

ALTER TABLE dbo.Calendar ADD PreviousDate AS 
case WHEN [Date]>'20100101' THEN  [Date]
    ELSE NULL
    END PERSISTED

MSDN clearly says that CASE is deterministic, here

However, I am getting an error:

Msg 4936, Level 16, State 1, Line 1 Computed column 'PreviousDate' in table 'Calendar' cannot be persisted because the column is non-deterministic.

Of course, i can create a scalar UDF and explicitly declare it as deterministic, but is there a simpler way around this? I am already in the middle of getting the latest service pack. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to CONVERT '20100101' with a style.

Source or target type is datetime or smalldatetime, the other source or target type is a character string, and a nondeterministic style is specified.

So, try this:

...WHEN [Date] > CONVERT(datetime, '20100101', 112)....

Date parsing from string can be unreliable as I've answered before (mostly in comments)

Edit:

I wouldn't say it's a bug, but SQL Server asking for 100% clarification. yyyymmdd is not ISO and SQL Server parsing yyyy-mm-dd is unreliable (see my answer link)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...