-
Notifications
You must be signed in to change notification settings - Fork 0
/
temporal_table.sql
73 lines (61 loc) · 2.35 KB
/
temporal_table.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
CREATE TABLE dbo.Users
(
UserID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_Users PRIMARY KEY,
UserName NVARCHAR(50) NOT NULL
);
CREATE TABLE dbo.Projects
(
ProjectID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_Projects PRIMARY KEY,
ProjectName NVARCHAR(50) NOT NULL
);
CREATE TABLE dbo.UserAssignment
(
AssignmentID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_UserAssignment PRIMARY KEY,
UserID INT NOT NULL CONSTRAINT FK_UserAssignment_Users_UserID FOREIGN KEY REFERENCES dbo.Users(UserID),
ProjectID INT NOT NULL CONSTRAINT FK_UserAssignment_Projects_ProjectID FOREIGN KEY REFERENCES dbo.Projects(ProjectID),
Amount DECIMAL(18,2) NOT NULL,
ValidFromUTC DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
ValidToUTC DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME (ValidFromUTC, ValidToUTC)
);
ALTER TABLE dbo.UserAssignment
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.UserAssignmentHistory));
INSERT INTO dbo.Users (UserName)
VALUES ('Wyatt Earp'),
('Doc Holliday'),
('Ike Clanton'),
('Virgil Earp');
INSERT INTO dbo.Projects (ProjectName)
VALUES ('Project A'),
('Project B'),
('Project C');
INSERT INTO dbo.UserAssignment (UserID, ProjectID, Amount)
VALUES
(1, 1, 5000.00),
(2, 2, 7500.00),
(3, 2, 6500.00),
(4, 1, 5500.00);
UPDATE dbo.UserAssignment
SET Amount = 6000.00
WHERE AssignmentID = 1;
UPDATE dbo.UserAssignment
SET Amount = 9000.00
WHERE AssignmentID = 2;
UPDATE dbo.UserAssignment
SET Amount = 6800.00
WHERE AssignmentID = 3;
INSERT INTO dbo.UserAssignment (UserID, ProjectID, Amount)
VALUES
(1, 3, 8000.00);
-- view the entire history of table
SELECT ua.AssignmentID, u.UserName, p.ProjectName, ua.Amount, ua.ValidFromUTC, ua.ValidToUTC
FROM dbo.UserAssignment FOR SYSTEM_TIME ALL ua
INNER JOIN dbo.Users u ON ua.UserID = u.UserID
INNER JOIN dbo.Projects p ON ua.ProjectID = p.ProjectID
ORDER BY AssignmentID, ValidFromUTC ASC;
-- view it for a set point in time
SELECT ua.AssignmentID, u.UserName, p.ProjectName, ua.Amount, ua.ValidFromUTC, ua.ValidToUTC
FROM dbo.UserAssignment FOR SYSTEM_TIME AS OF '2023-01-19 05:20:11.2282792' ua -- other options include FOR SYSTEM_TIME BETWEEN '2023-01-01' AND '2023-02-01'
INNER JOIN dbo.Users u ON ua.UserID = u.UserID
INNER JOIN dbo.Projects p ON ua.ProjectID = p.ProjectID
ORDER BY AssignmentID, ValidFromUTC ASC;