-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy path00-basic-sample.sql
111 lines (87 loc) · 3.34 KB
/
00-basic-sample.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
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
alter table dbo.OrderInfo set ( system_versioning = off );
drop table if exists dbo.OrderInfoHistory;
drop table if exists dbo.OrderInfo;
go
create table dbo.OrderInfo
(
id int not null primary key,
[description] nvarchar(1000) not null,
[value] money not null,
[received_on] datetime2 not null,
[status] varchar(100) not null,
customer_id varchar(10) not null,
valid_from datetime2 generated always as row start hidden not null,
valid_to datetime2 generated always as row end hidden not null,
period for system_time (valid_from, valid_to)
)
with (system_versioning = on (history_table = dbo.OrderInfoHistory))
go
insert into dbo.OrderInfo values
(1, 'My first order', 100, sysdatetime(), 'in-progress', 'DM'),
(2, 'Another Other', 200, sysdatetime(), 'in-progress', 'LR');
-- Time passes...
update dbo.OrderInfo set [status] = 'completed' where id = 1;
-- Time passes...
insert into dbo.OrderInfo values
(3, 'Another one', 300, sysdatetime(), 'in-progress', 'RC');
-- Time passes...
delete from dbo.OrderInfo where id = 2;
-- Analyize the data
select * from dbo.OrderInfo
select * from dbo.OrderInfoHistory
select *, valid_from, valid_to
from dbo.OrderInfo for system_time all
order by id, valid_from
alter table dbo.OrderInfo set ( system_versioning = off );
drop table if exists dbo.OrderInfoHistory;
drop table if exists dbo.OrderInfo;
-- create a temporal table with an explicitly specified history table
create table dbo.OrderInfo
(
id int not null primary key,
[description] nvarchar(1000) not null,
[value] money not null,
[received_on] datetime2 not null,
[status] varchar(100) not null,
customer_id varchar(10) not null,
valid_from datetime2 not null,
valid_to datetime2 not null
)
go
insert into dbo.OrderInfo values
(1, 'My first order', 100, '20210610', 'closed', 'DM', '20210610','9999-12-31 23:59:59.9999999')
go
alter table dbo.OrderInfo
add period for system_time ([valid_from], [valid_to])
go
alter table dbo.OrderInfo
set (system_versioning = on (history_table = dbo.OrderInfoHistory))
;
alter table dbo.OrderInfo
set ( system_versioning = off )
go
insert into dbo.OrderInfoHistory values
(1, 'My first order', 100, '20210601', 'received', 'DM', '20210601','20210602'),
(1, 'My first order', 100, '20210601', 'accepted', 'DM', '20210602','20210603'),
(1, 'My first order', 100, '20210601', 'processed', 'DM', '20210603','20210604'),
(1, 'My first order', 100, '20210601', 'blocked', 'DM', '20210604','20210605'),
(1, 'My first order', 100, '20210601', 'corrected', 'DM', '20210605','20210606'),
(1, 'My first order', 100, '20210601', 'processed', 'DM', '20210606','20210607'),
(1, 'My first order', 100, '20210601', 'prepared', 'DM', '20210607','20210608'),
(1, 'My first order', 100, '20210601', 'in-delivery', 'DM', '20210608','20210609'),
(1, 'My first order', 100, '20210601', 'delivered', 'DM', '20210609','20210610')
go
alter table dbo.OrderInfo
set ( system_versioning = on (history_table = dbo.OrderInfoHistory, data_consistency_check = on) )
go
select *
from dbo.OrderInfo for system_time all
where id = 1
order by valid_from
go
select * from dbo.OrderInfo where id = 1
go
select * from dbo.OrderInfo for system_time as of '2021-06-08' where id = 1 order by valid_from
go
select * from dbo.OrderInfo for system_time between '2021-06-08' and '2021-06-10' where id = 1 order by valid_from
go