I have a SQL query to add a computed column to a system versioned table. System versioned tables ofcourse don’t allow this, so I turn off system versioning first. Query below:
--Run in tran so system versioning resets on error
BEGIN TRAN
ALTER TABLE [dbo].[Table] SET (SYSTEM_VERSIONING = OFF);
ALTER TABLE [dbo].[Table] ADD [OBJID] AS SUBSTRING([OBJECTID],3,48) PERSISTED;
ALTER TABLE [dbo].[Table_history] ADD [OBJID] NVARCHAR(48) NULL;
ALTER TABLE [dbo].[Table] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[Table_history] , DATA_CONSISTENCY_CHECK = OFF ));
COMMIT TRAN
The issue is when I run this query as a whole it gives the following error:
System-versioned table schema modification failed because adding computed column while system-versioning is ON is not supported.
However when I run it step by step by just selecting each line and running the selection it works just as intended. I’ve been staring at this for way too long, any help would be greatly appreciated.