I am adding a computed column, which should be NOT NULL
on an existing table.
Both my source column for the computed value are NOT NULL
of course. I would like to make the computed column not persistent, to save space, but I fail to do so:
ALTER TABLE UnitHistory
ADD
[PrefixedUnitId] AS ((UnitHistory.UnitIdPrefix + cast(UnitHistory.UnitId
AS nvarchar(8)))) NOT NULL
GO
gives
Only UNIQUE or PRIMARY KEY constraints can be created on computed columns, while CHECK, FOREIGN KEY, and NOT NULL constraints require that computed columns be persisted. Msg 15135, Level 16, State 15, Procedure sp_addextendedproperty, Line 72 Object is invalid. Extended properties are not permitted on 'dbo.UnitHistory.PrefixedUnitId', or the object does not exist.
Now, I understand what the message says, and sure enough, using PERSISTED NOT NULL
does produce the column.
To save space however, I would like to have the column not be persisted.
Is there a way to have a computed column NOT NULL, but not persistent, when the underlying columns are NOT NULL?
Note: It should work on a SQL Server 2012 SP2