-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy path08-alter-temporal-table.sql
54 lines (45 loc) · 1.43 KB
/
08-alter-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
-- 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 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
-- DML with some sample data
insert into dbo.OrderInfo values
(1, 'My first order', 100, sysdatetime(), 'in-progress', 'DM'),
(2, 'Another Other', 200, sysdatetime(), 'in-progress', 'IBG');
go
alter table dbo.OrderInfo
add [shipped_on] datetime2 null
go
alter table dbo.OrderInfo
add [quantity] int not null default(0)
go
update dbo.OrderInfo set quantity = 10 where id = 1
go
insert into dbo.OrderInfo
(id, [description], [value], received_on, [status], customer_id, valid_from, valid_to, shipped_on, quantity)
values
(3, 'Yet another one', 300, sysdatetime(), 'received', 'DM', default, default, null, 10)
go
select * from dbo.[OrderInfo]
select * from dbo.[OrderInfoHistory]
go
alter table dbo.OrderInfo
drop constraint DF__OrderInfo__quant__3E1D39E1
go
alter table dbo.OrderInfo
drop column quantity
go
select * from dbo.[OrderInfo]
select * from dbo.[OrderInfoHistory]
go