I need to change the Existing Table Column, which is configured as GENERATED ALWAYS into a GENERATED BY DEFAULT.
Sample Table Structure
CREATE TABLE [dbo].[Contact](
[ContactID] [uniqueidentifier] NOT NULL,
[ContactNumber] [nvarchar](50) NOT NULL,
[SequenceID] [int] IDENTITY(1,1) NOT NULL,
[SysStartTime] [datetime2](0) GENERATED ALWAYS AS ROW START NOT NULL,
[SysEndTime] [datetime2](0) GENERATED ALWAYS AS ROW END NOT NULL,
CONSTRAINT [PK_Contact] PRIMARY KEY NONCLUSTERED
(
[ContactID] ASC
)WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY],
PERIOD FOR SYSTEM_TIME ([SysStartTime], [SysEndTime])
) ON [PRIMARY]
WITH
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[ContactHistory],
DATA_CONSISTENCY_CHECK = ON )
)
This is the table I'm already having now I need to change the column
[SysStartTime] to GENERATED BY DEFAULT from GENERATED ALWAYS
I tried the following code
ALTER TABLE dbo.Contact ALTER column SysStartTime SET GENERATED BY DEFAULT
But it throws an error
Msg 156, Level 15, State 1, Line 19 Incorrect syntax near the keyword 'SET'.
Kindly assist me.
I know this is an old question, but I stumbled on a similar problem and I found a way to achieve an end result equivalent to the one desired here (have the records tell the correct story of when they were created and also properly work with temporal queries).
What I did was:
ALTER TABLE Contact SET (SYSTEM_VERSIONING = OFF);ALTER TABLE Contact SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[ContactHistory], DATA_CONSISTENCY_CHECK = ON ))This causes the records to be identical (except for the ValidFrom/To fields), but allow for any query using
FOR SYSTEM_TIME AS OF 'YYYY-MM-DD THH:mm:SS.0000000';to work properly and represent the data exactly as it was in the moment requested.Things I found out when trying this:
GENERATED ALWAYS AS ROW START/END. It needs to be a field set that way from the beginning.Additional documentation: https://learn.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned-temporal-table?view=sql-server-ver15