-
Notifications
You must be signed in to change notification settings - Fork 36
/
01-ct-sample.sql
114 lines (98 loc) · 1.83 KB
/
01-ct-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
112
113
114
/*
View Data
*/
select * from dbo.TrainingSessions
go
/*
Get current version number
*/
select change_tracking_current_version()
go
/*
Make some changes
*/
insert into dbo.TrainingSessions
(Id, RecordedOn, [Type], Steps, Distance, Duration, Calories)
values
(3, '20211020 18:24:32 -08:00', 'Run', 4866, 4562, 30*60+18, 475)
go
update
dbo.TrainingSessions
set
Steps = 3450
where
Id = 2
go
/*
Delete something
*/
delete from dbo.TrainingSessions where Id = 1
go
/*
View Data
*/
select * from dbo.TrainingSessions
go
/*
Get the current version
*/
select change_tracking_current_version()
go
/*
Return the changes from the requested version
*/
declare @fromVersion int = 78;
select
sys_change_version, sys_change_operation, Id
from
changetable(changes dbo.TrainingSessions, @fromVersion) C
go
declare @fromVersion int = 78;
select
sys_change_version, sys_change_operation,
t.*
from
changetable(changes dbo.TrainingSessions, @fromVersion) C
left outer join
dbo.TrainingSessions t on c.Id = t.Id
go
/*
Add couple of more rows
*/
begin tran
go
insert into dbo.TrainingSessions
(Id, RecordedOn, [Type], Steps, Distance, Duration, Calories)
values
(4, '20211021 07:35:14 -08:00', 'Run', 4123, 4234, 30*60+23, 411),
(5, '20211021 16:32:54 -08:00', 'Run', 4568, 4780, 30*60+44, 4890);
go
update dbo.TrainingSessions
set Calories = 489
where Id = 5
go
/*
Update Session Id 5 again
*/
update dbo.TrainingSessions
set Calories = 563
where Id = 5
go
commit tran
go
select @@trancount
go
/*
Get the current version
*/
select change_tracking_current_version()
go
declare @fromVersion int = 82;
select
sys_change_version, sys_change_operation,
t.*
from
changetable(changes dbo.TrainingSessions, @fromVersion) C
left outer join
dbo.TrainingSessions t on c.Id = t.Id
go