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.
While this is not a direct answer to this old question, I've been met with a problem talked about by Kelps Sousa Alux, where I needed to have a table column not be 'generated always' in the start so that I could explicitly insert the ValidFrom/To values, but after a series of other operations those columns needed to be end up as 'generated always'.
While not directly, it is possible to do so
First create a table and the history table separately, insert the data you need, then add the PERIOD FOR SYSTEM_TIME, set the columns as hidden if you wish to, then set the system versioning on.
If you ever need to disable the temporal table, insert data explicitly to all columns, and then reattach the history table to the temporal one, you can also do that - like so:
Remember about identity insert, if you need it. If after this you check the
generated_always_type
andgenerated_always_type_desc
columns insys.all_columns
, you will see that they are set appropriately toAS_ROW_START/END