0

I am getting the following error:

Computed column 'OrderIssue' in table 'ProductIssue' cannot be persisted because the column is non-deterministic.

when trying to create a table with a column like

[OrderIssue]  AS 
(case 
    when [IssueID]=(1009) AND 
         ([Issuenumber]>=(400000) AND 
          [Issuenumber]<=(999999) OR 
          [Issuenumber]<(400000) AND 
          isnull(isnull([Enterdate],[Stagedate]),[lastClosingdate])<
                        dateadd(day,(0),'2013-04-01')) 
    then (0) 
    when [IssueID]=(1009) AND 
         ([Issuenumber]>=(400000) OR 
          [Issuenumber]<(700000) AND 
          isnull(isnull([Enterdate],[Stagedate]),[lastclosingdate])>=
                        dateadd(day,(0),'2013-04-01')) 
    then (1)  
end) PERSISTED

Please suggest me how to fix this.

4
  • 3
    What an absolutely bizarre design. Are you sure you need to have this in a persisted column? What are you actually trying to achieve? Commented Dec 22, 2016 at 16:22
  • 1
    From the msdn A computed column is a virtual column that is not physically stored in the table, unless the column is marked PERSISTED. A computed column expression can use data from other columns to calculate a value for the column to which it belongs.. Commented Dec 22, 2016 at 16:23
  • It looks like you are trying to use a persisted column where a view would be appropriate. Commented Dec 22, 2016 at 16:25
  • 7
    You don't have an Else in the Case statement. What happens if neither statement is true? That's why you are getting a non-deterministic error. Commented Dec 22, 2016 at 16:25

1 Answer 1

2

Your date columns make it non-deterministic. try turning them into ints like this:

[OrderIssue] AS (case when [IssueID]=(1009) AND ([Issuenumber]>=(400000) AND [Issuenumber]<=(999999) OR [Issuenumber]<(400000) AND cast(convert(varchar(8),isnull(isnull([Enterdate],[Stagedate]),[lastClosingdate]),112) as int)=(400000) OR [Issuenumber]<(700000) AND cast(convert(varchar(8),isnull(isnull([Enterdate],[Stagedate]),[lastClosingdate]),112) as int)>=20130401) then (1) end) PERSISTED)

Or you can run this other test:

if OBJECT_ID('tempdb..#tb_Test') is not null
drop table #tb_Test;

create table #tb_Test
(
IssueID int not null,
IssueNumber int not null,
Enterdate date not null,
Stagedate date not null,
lastClosingDate date not null,
[OrderIssue]  AS (
    case 
        when cast(convert(varchar(8),[Enterdate],112) as int) >= 20161222 then 0 else 1 end

) PERSISTED)

insert into #tb_Test values(1,1,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)
insert into #tb_Test values(1,1,'20100101','20110101','20120101')

select *, cast(convert(varchar(8),[Enterdate],112) as int) from #tb_Test

cheers,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.