Skip to content

Instantly share code, notes, and snippets.

@mocella
Last active August 24, 2018 18:56
Show Gist options
  • Select an option

  • Save mocella/205168cc4a177a2ae1ac5925ebe9934c to your computer and use it in GitHub Desktop.

Select an option

Save mocella/205168cc4a177a2ae1ac5925ebe9934c to your computer and use it in GitHub Desktop.
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');
ALTER TABLE [dbo].[Employee]
SET
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[EmployeeHistory])
);
ALTER TABLE dbo.Employee ALTER COLUMN [LastName] nvarchar(255) NOT NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment