Cannot persist computed column - not deterministic

后端 未结 1 1029
渐次进展
渐次进展 2020-12-10 12:24

I have this function for a computed column :

CREATE FUNCTION [dbo].[GetAllocatedStartTime](@Year INT, @Week INT)
RETURNS DATETIME

WITH schemabinding
AS BEGI         


        
相关标签:
1条回答
  • 2020-12-10 12:58

    CONVERT([varchar](4),@Year,(0))+'-01-01' is being passed to a DATEDIFF call, in a position where a date is expected, forcing an implicit conversion to occur.

    From the rules for deterministic functions:

    CAST

    Deterministic unless used with datetime, smalldatetime, or sql_variant.

    CONVERT

    Deterministic unless one of these conditions exists:

    ...

    Source or target type is datetime or smalldatetime, the other source or target type is a character string, and a nondeterministic style is specified. To be deterministic, the style parameter must be a constant. Additionally, styles less than or equal to 100 are nondeterministic, except for styles 20 and 21. Styles greater than 100 are deterministic, except for styles 106, 107, 109 and 113.

    Well, you're calling neither, but you're relying on an implicit conversion, which I'd expect to act like CAST. Rather than rely on this, I'd switch to using CONVERT and give a deterministic style parameter.

    So, I'd do: CONVERT(datetime,CONVERT([varchar](4),@Year,(0))+'0101',112) in its place. Having done so, the function itself becomes deterministic

    0 讨论(0)
提交回复
热议问题