Computed column 'Month' in table cannot be persisted because the column is non-deterministic
I am getting one error when I try to make my field IsPersisted=True
This is the definition of my table
CREATE TABLE [dbo].[AdvanceMST](
[AdvanceID] [bigint] IDENTITY(1,1) NOT NULL,
[AppliedDate] [datetime] NULL,
[ApprovedDate] [datetime] NULL,
[AdvanceStatus] [varchar](100) NULL,
[AdvanceFromEngineerID] [bigint] NULL,
[AdvanceToEngineerID] [bigint] NULL,
[AccountResourceID] [bigint] NULL,
[AdvanceAmount] [float] NULL,
[ApprovedAdvanceAmount] [float] NULL,
[POrderID] [bigint] NULL,
[SiteID] [bigint] NULL,
CONSTRAINT [PK_AdvanceMST] PRIMARY KEY CLUSTERED
(
[AdvanceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMA开发者_StackOverflow中文版RY]
This is the error message Computed column 'Month' in table 'Tmp_AdvanceMST' cannot be persisted because the column is non-deterministic.
I am not able to understand what does this error message mean and how to solve this problem in order to make it IsPersisted=true
The datename
function can return different results dependant on the language of the logged in user hence is not deterministic. Marking a computed column as persisted means SQL Server stores the results of the computation which requires there to be exactly one result.
If you need this as persisted at all you can replace with a 12 branch case
expression with the specific language you want used.
Although it would probably be all round better to have month(AppliedDate)
as a persisted integer column and have the datename
function in a non persisted computed column.
Edited to give example as per comments
CREATE TABLE #T
(
[AppliedDate] DATETIME,
[Month] AS CASE MONTH([AppliedDate])
WHEN 1 THEN 'January'
WHEN 2 THEN 'February'
WHEN 3 THEN 'March'
WHEN 4 THEN 'April'
WHEN 5 THEN 'May'
WHEN 6 THEN 'June'
WHEN 7 THEN 'July'
WHEN 8 THEN 'August'
WHEN 9 THEN 'September'
WHEN 10 THEN 'October'
WHEN 11 THEN 'November'
WHEN 12 THEN 'December'
END PERSISTED
)
INSERT INTO #T([AppliedDate]) VALUES (GETDATE())
精彩评论