I am using Sql Server 2014
CREATE TABLE [dbo].[Record](
[RecordId] [int] IDENTITY(1,1) NOT NULL,
[BookId] [int] NULL,
[TopicId] [int] NULL,
[BookName] [varchar](250) NULL,
[TopicName] [varchar](250) NULL,
[RecordPath] [varchar](250) NULL,
[RecordName] [varchar](250) NULL,
[DurationInSec] [float] NULL,
CONSTRAINT [PK_Record] PRIMARY KEY CLUSTERED
(
[RecordId] ASC
)
ALTER TABLE dbo.Record ADD CONSTRAINT DC_RecordPath DEFAULT
(CASE WHEN BookId = 1 THEN 'path/to/1' ELSE RecordPath END)
I want a default constraint with condition as above, but this is not permitted. Is there any workaround without using triggers?
Edit: Computed column is not working, because the column is filled by a few stored procedures. I have two conditions;
- If
BookIdis 1 thenRecordPathis path 1 - Else for any
BookId,RecordPathisRecordPathcomes from stored procedure
DEFAULTvalue or aCONSTRAINTto make sure the value is always correct. If compute column doesnt work. You can use aTRIGGER