tsql - Computed Column - as Primary key -


i have following computed column within create table function:

create table test ( code1 nchar(10), code2 nchar (10), type nchar(10), final case when len(code1)>0 code1 when len(code2)>0 code2 else type end );  alter table test add constraint pk_test1 primary key (final) 

but getting following error

"cannot define primary key constraint on nullable column in table".

this because code 1, code 2 , type can null, 3 never null. 1 of them have value.

is there anyway can define computed column primary key above in mind?

many - jt

your computed column needs persisted , not null used primary key;

create table test (     code1 nchar(10),     code2 nchar (10),     type nchar(10),     final case when len(code1)>0 code1                    when len(code2)>0 code2                    else type end            persisted not null ); 

an sqlfiddle test with.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -