-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqlserver2022-security-ledger1_updatable.sql
267 lines (177 loc) · 6.44 KB
/
sqlserver2022-security-ledger1_updatable.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
SQL Server 2022
SQL Ledger : Updatable table, system views
https://www.kursysql.pl
*/
USE AdventureWorks2019
GO
/*
Tworzenie tabeli i wypełnianie danymi
*/
DROP TABLE IF EXISTS dbo.EmployeeRate
-- utworzenie nowej tabeli typu ledger i wypełnienie jej danymi
CREATE TABLE dbo.EmployeeRate (
BusinessEntityID int PRIMARY KEY IDENTITY,
Firstname nvarchar(50),
Lastname nvarchar(50),
HireDate date,
JobTitle nvarchar(50),
Rate money,
PayFrequency tinyint
)
WITH
(
SYSTEM_VERSIONING = ON,
LEDGER = ON
)
-- ObjectExplorer: lista tabel, nowe ikonki...
DROP TABLE IF EXISTS dbo.EmployeeRate
GO
CREATE TABLE dbo.EmployeeRate (
ID int PRIMARY KEY IDENTITY,
Firstname nvarchar(50),
Lastname nvarchar(50),
HireDate date,
JobTitle nvarchar(50),
Rate money,
PayFrequency tinyint
)
WITH
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.EmployeeRateHistory),
LEDGER = ON
)
;WITH _cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY BusinessEntityID ORDER BY RateChangeDate DESC) AS Rn
FROM HumanResources.EmployeePayHistory
)
INSERT INTO dbo.EmployeeRate(FirstName, LastName, HireDate, JobTitle, Rate, PayFrequency)
SELECT TOP 10 p.FirstName, p.LastName, emp.HireDate, emp.JobTitle, _cte.Rate, _cte.PayFrequency
FROM _cte
JOIN HumanResources.Employee AS emp ON emp.BusinessEntityID = _cte.BusinessEntityID
JOIN Person.Person AS p ON p.BusinessEntityID = emp.BusinessEntityID
WHERE _cte.Rn = 1
/*
Odczyt danych
*/
SELECT * FROM dbo.EmployeeRate
SELECT * FROM dbo.EmployeeRateHistory
-- nowe kolumny: ledger_type, ledger_type_desc, ledger_view_id, is_dropped_ledger_table
SELECT * FROM sys.tables
WHERE name like 'Employee%'
ORDER BY name
SELECT
ID, Firstname, Lastname, HireDate, JobTitle, Rate, PayFrequency,
ledger_start_transaction_id, ledger_end_transaction_id,
ledger_start_sequence_number, ledger_end_sequence_number
FROM dbo.EmployeeRate
-- wstawienie kolejnych 2 wierszy
;WITH _cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY BusinessEntityID ORDER BY RateChangeDate DESC) AS Rn
FROM HumanResources.EmployeePayHistory
)
INSERT INTO dbo.EmployeeRate(FirstName, LastName, HireDate, JobTitle, Rate, PayFrequency)
SELECT TOP 2 p.FirstName, p.LastName, emp.HireDate, emp.JobTitle, _cte.Rate, _cte.PayFrequency
FROM _cte
JOIN HumanResources.Employee AS emp ON emp.BusinessEntityID = _cte.BusinessEntityID
JOIN Person.Person AS p ON p.BusinessEntityID = emp.BusinessEntityID
WHERE _cte.Rn = 1 AND emp.BusinessEntityID > 200
SELECT
ID, Firstname, Lastname, HireDate, JobTitle, Rate, PayFrequency,
ledger_start_transaction_id, ledger_end_transaction_id,
ledger_start_sequence_number, ledger_end_sequence_number
FROM dbo.EmployeeRate
/*
Ledger view
*/
SELECT * FROM sys.views
WHERE name like 'Employee%'
-- lista wszystkich tabel ledger, tabel z historią i widoków
SELECT
ts.[name] + '.' + t.[name] AS [ledger_table_name]
, hs.[name] + '.' + h.[name] AS [history_table_name]
, vs.[name] + '.' + v.[name] AS [ledger_view_name]
FROM sys.tables AS t
JOIN sys.tables AS h ON (h.[object_id] = t.[history_table_id])
JOIN sys.views v ON (v.[object_id] = t.[ledger_view_id])
JOIN sys.schemas ts ON (ts.[schema_id] = t.[schema_id])
JOIN sys.schemas hs ON (hs.[schema_id] = h.[schema_id])
JOIN sys.schemas vs ON (vs.[schema_id] = v.[schema_id])
SELECT * FROM EmployeeRate_Ledger
EXEC sp_helptext 'EmployeeRate_Ledger'
GO
-- definicja wygenerowanego widoku - zawiera informacje o wstawionych wierszach,
-- a także historii zmian (UPDATE jako DELETE i INSERT)
CREATE VIEW [dbo].[EmployeeRate_Ledger] AS
SELECT [ID], [Firstname], [Lastname], [HireDate], [JobTitle], [Rate], [PayFrequency],
[ledger_start_transaction_id] AS [ledger_transaction_id], [ledger_start_sequence_number] AS [ledger_sequence_number],
1 AS [ledger_operation_type], N'INSERT' AS [ledger_operation_type_desc]
FROM [dbo].[EmployeeRate]
UNION ALL
SELECT [ID], [Firstname], [Lastname], [HireDate], [JobTitle], [Rate], [PayFrequency],
[ledger_start_transaction_id] AS [ledger_transaction_id], [ledger_start_sequence_number] AS [ledger_sequence_number],
1 AS [ledger_operation_type], N'INSERT' AS [ledger_operation_type_desc]
FROM [dbo].[EmployeeRateHistory]
UNION ALL
SELECT [ID], [Firstname], [Lastname], [HireDate], [JobTitle], [Rate], [PayFrequency],
[ledger_end_transaction_id] AS [ledger_transaction_id], [ledger_end_sequence_number] AS [ledger_sequence_number],
2 AS [ledger_operation_type], N'DELETE' AS [ledger_operation_type_desc]
FROM [dbo].[EmployeeRateHistory]
/*
Modyfikowanie danych
*/
UPDATE dbo.EmployeeRate SET Rate = Rate*1.5 WHERE ID = 1
UPDATE dbo.EmployeeRate SET Rate = Rate*2 WHERE ID = 2
SELECT * FROM dbo.EmployeeRate
SELECT
ID, Firstname, Lastname, HireDate, JobTitle, Rate, PayFrequency,
ledger_start_transaction_id, ledger_end_transaction_id,
ledger_start_sequence_number, ledger_end_sequence_number
FROM dbo.EmployeeRate
-- w tabeli z historią - poprzednia wersja zmodyfikowanych wierszy
SELECT * FROM dbo.EmployeeRateHistory
-- kasowanie danych
DELETE FROM EmployeeRate WHERE ID = 10
SELECT * FROM dbo.EmployeeRate
SELECT
ID, Firstname, Lastname, HireDate, JobTitle, Rate, PayFrequency,
ledger_start_transaction_id, ledger_end_transaction_id,
ledger_start_sequence_number, ledger_end_sequence_number
FROM dbo.EmployeeRate
-- w tabeli z historią - skasowany wiersz
SELECT * FROM dbo.EmployeeRateHistory
-- widok prezentujący rodzaj operacji,
-- posortowanych chronologicznie, tj zgodnie z identyfikatorami transkacji
SELECT * FROM EmployeeRate_Ledger
ORDER BY ledger_transaction_id, ledger_sequence_number
-- Msg 13545, Level 16, State 1, Line 216
-- Truncate failed on table 'AdventureWorks2019.dbo.EmployeeRate' because
-- it is not a supported operation on system-versioned tables.
TRUNCATE TABLE dbo.EmployeeRate
/*
sys.database_ledger_transactions
*/
SELECT * FROM sys.database_ledger_transactions
SELECT
t.commit_time
,t.principal_name
,ID, Firstname, Lastname, HireDate, JobTitle, Rate, PayFrequency
,e.ledger_operation_type_desc
,e.ledger_transaction_id
FROM dbo.EmployeeRate_Ledger AS e
JOIN sys.database_ledger_transactions AS t
ON t.transaction_id = e.ledger_transaction_id
ORDER BY ledger_transaction_id, ledger_sequence_number
/*
Kasowanie tabeli ledger
*/
DROP TABLE dbo.EmployeeRate
GO
SELECT * FROM sys.tables
WHERE name like 'Employee%'
ORDER BY name
-- MSSQL_DroppedLedgerHistory_EmployeeRateHistory_GUID
SELECT * FROM sys.tables
WHERE name like '%Employee%'
ORDER BY name