-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32_Temporal_Tables.sql
More file actions
151 lines (134 loc) · 4.13 KB
/
32_Temporal_Tables.sql
File metadata and controls
151 lines (134 loc) · 4.13 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**************************************************************
* SQL Server 2022 Temporal Tables Tutorial
* Description: This script demonstrates how to create and use
* system-versioned temporal tables in SQL Server 2022.
* It covers:
* - Creating a temporal table with a history table.
* - Inserting, updating, and deleting data to generate history.
* - Querying current data, full history, and data as of specific times.
* - Disabling and re-enabling system versioning.
* - Cleanup instructions.
**************************************************************/
-------------------------------------------------
-- Region: 1. Cleanup Existing Tables (for re-run purposes)
-------------------------------------------------
IF OBJECT_ID('dbo.EmployeeHistory', 'U') IS NOT NULL
DROP TABLE dbo.EmployeeHistory;
IF OBJECT_ID('dbo.Employee', 'U') IS NOT NULL
DROP TABLE dbo.Employee;
GO
-------------------------------------------------
-- Region: 2. Creating the Temporal Table
-------------------------------------------------
/*
Create the temporal table with system versioning.
The ValidFrom and ValidTo columns are generated automatically.
*/
CREATE TABLE dbo.Employee
(
EmployeeID INT NOT NULL PRIMARY KEY,
Name NVARCHAR(100) NOT NULL,
Position NVARCHAR(100) NOT NULL,
Salary DECIMAL(10, 2) NOT NULL,
ValidFrom DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
ValidTo DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
)
WITH
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.EmployeeHistory)
);
GO
-------------------------------------------------
-- Region: 3. Inserting and Modifying Data
-------------------------------------------------
/*
Insert initial data into the temporal table.
*/
INSERT INTO dbo.Employee (EmployeeID, Name, Position, Salary)
VALUES
(1, 'Alice', 'Manager', 90000.00),
(2, 'Bob', 'Developer', 75000.00),
(3, 'Charlie', 'Analyst', 68000.00);
GO
-- Wait for a short period to ensure different timestamps
WAITFOR DELAY '00:00:02';
GO
/*
Update data: Increase Bob's salary and change his Position.
*/
UPDATE dbo.Employee
SET Salary = 80000.00, Position = 'Senior Developer'
WHERE EmployeeID = 2;
GO
WAITFOR DELAY '00:00:02';
GO
/*
Another update: Modify Alice's salary.
*/
UPDATE dbo.Employee
SET Salary = 95000.00
WHERE EmployeeID = 1;
GO
/*
Delete an employee (Charlie) to generate a delete history record.
*/
DELETE FROM dbo.Employee
WHERE EmployeeID = 3;
GO
-------------------------------------------------
-- Region: 4. Querying Temporal Data
-------------------------------------------------
/*
4.1 Query the current data.
*/
SELECT *
FROM dbo.Employee;
GO
/*
4.2 Query the full history (current and historical rows).
*/
SELECT *
FROM dbo.Employee
FOR SYSTEM_TIME ALL;
GO
/*
4.3 Query as of a specific point in time.
(Replace @AsOfTime with an appropriate value from your history.)
*/
DECLARE @AsOfTime DATETIME2 = DATEADD(SECOND, -3, SYSUTCDATETIME());
SELECT *
FROM dbo.Employee
FOR SYSTEM_TIME AS OF @AsOfTime;
GO
/*
4.4 Query changes between two time points.
*/
DECLARE @StartTime DATETIME2 = DATEADD(SECOND, -10, SYSUTCDATETIME());
DECLARE @EndTime DATETIME2 = SYSUTCDATETIME();
SELECT *
FROM dbo.Employee
FOR SYSTEM_TIME BETWEEN @StartTime AND @EndTime;
GO
-------------------------------------------------
-- Region: 5. Disabling System-Versioning (Optional)
-------------------------------------------------
/*
To temporarily disable system-versioning:
ALTER TABLE dbo.Employee SET (SYSTEM_VERSIONING = OFF);
After modifications, re-enable using:
ALTER TABLE dbo.Employee SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.EmployeeHistory));
*/
GO
-------------------------------------------------
-- Region: 6. Cleanup (Optional)
-------------------------------------------------
/*
Uncomment the following lines to drop the tables after testing.
*/
-- DROP TABLE dbo.Employee;
-- DROP TABLE dbo.EmployeeHistory;
GO
-------------------------------------------------
-- End of Script
-------------------------------------------------