-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path05-stored-proc-update.sql
More file actions
58 lines (52 loc) · 1.87 KB
/
05-stored-proc-update.sql
File metadata and controls
58 lines (52 loc) · 1.87 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
create or alter procedure web.get_trainingsessionsync
@json nvarchar(max)
as
declare @fromVersion int = json_value(@json, '$.fromVersion')
set xact_abort on
set transaction isolation level snapshot;
begin tran
declare @reason int
declare @curVer int = change_tracking_current_version();
declare @minVer int = change_tracking_min_valid_version(object_id('dbo.TrainingSession'));
if (@fromVersion = 0) begin
set @reason = 0 -- First Sync
end else if (@fromVersion < @minVer) begin
set @fromVersion = 0
set @reason = 1 -- fromVersion too old. New full sync needed
end
if (@fromVersion = 0)
begin
select
@curVer as 'Metadata.Sync.Version',
'Full' as 'Metadata.Sync.Type',
@reason as 'Metadata.Sync.ReasonCode',
[Data] = json_query((select Id, RecordedOn, [Type], Steps, Distance, Temperature from dbo.TrainingSession for json auto))
for
json path, without_array_wrapper
end else begin
select
@curVer as 'Metadata.Sync.Version',
'Diff' as 'Metadata.Sync.Type',
[Data] = json_query((
select
ct.SYS_CHANGE_OPERATION as '$operation',
ct.Id,
ts.RecordedOn,
ts.[Type],
ts.Steps,
ts.Distance,
ts.PostProcessedOn,
ts.AdjustedSteps,
ts.AdjustedDistance,
ts.Temperature
from
dbo.TrainingSession as ts
right outer join
changetable(changes dbo.TrainingSession, @fromVersion) as ct on ct.[Id] = ts.[id]
for
json path
))
for
json path, without_array_wrapper
end
commit tran