Skip to content

Instantly share code, notes, and snippets.

@mocella
mocella / AlterTemporalTableColumn.sql
Last active August 24, 2018 18:56
Steps to modify a column in a temporal table in Sql Server
/*
Steps to alter a column in a Temporal Table
e.g. Table "Employee" versioned by "EmployeeHistory", change column "LastName" from Null to Not Null
*/
ALTER TABLE [dbo].[Employee] SET (SYSTEM_VERSIONING = OFF);
-- fix any null entries in the primary and history tables:
UPDATE dbo.Employee SET [LastName] = '' WHERE [LastName] IS NULL;
EXEC('UPDATE dbo.EmployeeHistory SET [LastName] = '''' WHERE [LastName] IS NULL');