Skip to content

Instantly share code, notes, and snippets.

@Siphonophora
Created May 6, 2020 03:08
Show Gist options
  • Save Siphonophora/0ca0c99cedf6f4bb67ea3c40f0c9c798 to your computer and use it in GitHub Desktop.
Save Siphonophora/0ca0c99cedf6f4bb67ea3c40f0c9c798 to your computer and use it in GitHub Desktop.
CREATE TABLE [ExampleTable]
(
[ID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY
, [SysStartTime] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL
, [SysEndTime] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL
, [InsertTime] [datetime2](7) NOT NULL
, [TransactionType] [varchar](20) NOT NULL
, PERIOD FOR SYSTEM_TIME ([SysStartTime], [SysEndTime])
)
WITH ( SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[ExampleTableHistory] , DATA_CONSISTENCY_CHECK = ON ));
GO
INSERT INTO [ExampleTable] (InsertTime, TransactionType)
VALUES (SYSUTCDATETIME(), 'Implicit');
WAITFOR DELAY '00:00:01';
INSERT INTO [ExampleTable] (InsertTime, TransactionType)
VALUES (SYSUTCDATETIME(), 'Implicit Multi Row')
, (SYSUTCDATETIME(), 'Implicit Multi Row');
Begin Tran
WAITFOR DELAY '00:00:01';
INSERT INTO [ExampleTable] (InsertTime, TransactionType)
VALUES (SYSUTCDATETIME(), 'Explicit');
Begin Tran
WAITFOR DELAY '00:00:01';
INSERT INTO [dbo].[ExampleTable] (InsertTime, TransactionType)
VALUES (SYSUTCDATETIME(), 'Explicit (Nested)');
Commit Tran
Commit Tran
Select
*
, DateDiff(ms, SysStartTime , InsertTime) SysStartToInsert_ms
From [dbo].[ExampleTable] for system_time all
ALTER TABLE [dbo].[ExampleTable] Set (SYSTEM_VERSIONING = OFF);
Drop table [dbo].[ExampleTable]
Drop table [dbo].[ExampleTableHistory]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment