Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save James-DBA-Anderson/03a73277f08a3df72d658ba347715256 to your computer and use it in GitHub Desktop.
Save James-DBA-Anderson/03a73277f08a3df72d658ba347715256 to your computer and use it in GitHub Desktop.
CREATE TABLE dbo.Orders
(
[OrderId] INT NOT NULL PRIMARY KEY CLUSTERED
, [OrderValue] DECIMAL(19,4)
, [ValidFrom] DATETIME2 (2) GENERATED ALWAYS AS ROW START
, [ValidTo] DATETIME2 (2) GENERATED ALWAYS AS ROW END
, PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.OrdersHistory));
GO
INSERT dbo.Orders ([OrderId], [OrderValue])
VALUES (1, 9.99), (2, 9.99);
GO
SELECT * FROM dbo.Orders;
GO
BEGIN TRAN
WAITFOR DELAY '00:00:30';
UPDATE dbo.Orders
SET [OrderValue] = [OrderValue] + 1;
COMMIT TRAN
GO
-- Run this in another query tab before the transaction above completes
SELECT GETUTCDATE() AS 'RunTime', * FROM dbo.Orders;
-----------------------------------------------------------------------
SELECT * FROM dbo.Orders
FOR SYSTEM_TIME AS OF 'PASTE THE RUNTIME VALUE FROM THE QUERY ABOVE';
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment