-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
TSQL_data_lineage.sql
374 lines (282 loc) · 9.46 KB
/
TSQL_data_lineage.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
USE lineage;
GO
/* ******************************
*
* 1. Remove unnecessary characters
*
******************************** */
CREATE OR ALTER FUNCTION dbo.fn_removelistChars
/*
Author: Tomaz Kastrun
Created: 06.JUN.2022
Desc: Function for removing list of unwanted characters
Usage:
SELECT dbo.fn_removelistChars('Tol~99""''''j\e.j/e[,t&eks]t,ki')
*/
(
@txt AS VARCHAR(max)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @list VARCHAR(200) = '%[^a-zA-Z0-9+@#\/%=_?!:.''-]%'
WHILE PATINDEX(@list,@txt) > 0
SET @txt = REPLACE(@txt,SUBSTRING(@txt,PATINDEX(@list,@txt),1),'')
RETURN @txt
END;
GO
CREATE OR ALTER PROCEDURE dbo.TSQL_data_lineage
/*
Author: Tomaz Kastrun
Date: August 2022
GitHub: github.com/tomaztk
Blogpost:
Description:
Removing all comments from your T-SQL Query for a given procedure for better code visibility and readability - separate function
Remove all unused characters.
Create data lineage for inputed T-SQL query
Usage:
EXEC dbo.TSQL_data_lineage
@InputQuery = N' SELECT * FROM master.dbo.spt_values '
*/
(
@InputQuery NVARCHAR(MAX)
)
AS
BEGIN
/* ******************************
*
* 2. Remove comments characters
*
******************************** */
DROP TABLE IF EXISTS dbo.SQL_query_table
CREATE TABLE dbo.SQL_query_table (
id INT IDENTITY(1,1) NOT NULL
,query_txt NVARCHAR(4000)
)
-- Breaks the procedure into lines with linebreak
-- INSERT INTO dbo.SQL_query_table
-- EXEC sp_helptext
-- @objname = @InputQuery
-- Breaks the query into lines with linebreak
DECLARE @MAX_nof_break INT = (select len(@InputQuery) - len(replace(@InputQuery, CHAR(10), '')))
DECLARE @start_nof_break INT = 1
declare @iq2 NVARCHAR(max) = @InputQuery
declare @max_len int = (SELECT len(@InputQuery))
declare @start_pos int = 0
declare @br_pos int = 0
while (@MAX_nof_break >= @start_nof_break)
BEGIN
SET @br_pos = (SELECT charindex( char(10), @iq2) )
INSERT INTO dbo.SQL_query_table(query_txt)
SELECT substring(@InputQuery,@start_pos, @br_pos )
SET @start_pos = @start_pos + @br_pos
SET @iq2 = SUBSTRING(@InputQuery, @start_pos, @max_len)
SET @start_nof_break = @start_nof_break + 1
END
--- STart removing comments
DECLARE @proc_text varchar(MAX) = ''
DECLARE @proc_text_row varchar(MAX)
DECLARE @proc_no_comment varchar(MAX) = ''
DECLARE @comment_count INT = 0
SELECT @proc_text = @proc_text + CASE
WHEN LEN(@proc_text) > 0 THEN '\n'
ELSE '' END + query_txt
FROM dbo.SQL_query_table
DECLARE @i INT = 1
DECLARE @rowcount INT = (SELECT LEN(@proc_text))
WHILE (@i <= @rowcount)
BEGIN
IF SUBSTRING(@proc_text,@i,2) = '/*'
BEGIN
SELECT @comment_count = @comment_count + 1
END
ELSE IF SUBSTRING(@proc_text,@i,2) = '*/'
BEGIN
SELECT @comment_count = @comment_count - 1
END
ELSE IF @comment_count = 0
SELECT @proc_no_comment = @proc_no_comment + SUBSTRING(@proc_text,@i,1)
IF SUBSTRING(@proc_text,@i,2) = '*/'
SELECT @i = @i + 2
ELSE
SELECT @i = @i + 1
END
WHILE (@i <= @rowcount)
BEGIN
IF SUBSTRING(@proc_text,@i,4) = '/*/*'
BEGIN
SELECT @comment_count = @comment_count + 2
END
ELSE IF SUBSTRING(@proc_text,@i,4) = '*/*/'
BEGIN
SELECT @comment_count = @comment_count - 2
END
ELSE IF @comment_count = 0
SELECT @proc_no_comment = @proc_no_comment + SUBSTRING(@proc_text,@i,1)
IF SUBSTRING(@proc_text,@i,4) = '*/*/'
SELECT @i = @i + 2
ELSE
SELECT @i = @i + 1
END
DROP TABLE IF EXISTS #tbl_sp_no_comments
CREATE TABLE #tbl_sp_no_comments (
rn INT IDENTITY(1,1)
,sp_text VARCHAR(8000)
)
WHILE (LEN(@proc_no_comment) > 0)
BEGIN
INSERT INTO #tbl_sp_no_comments (sp_text)
SELECT SUBSTRING( @proc_no_comment, 0, CHARINDEX('\n', @proc_no_comment))
SELECT @proc_no_comment = SUBSTRING(@proc_no_comment, CHARINDEX('\n',@proc_no_comment) + 2, LEN(@proc_no_comment))
END
DROP TABLE IF EXISTS #tbl_sp_no_comments_fin
CREATE TABLE #tbl_sp_no_comments_fin
(rn_orig INT IDENTITY(1,1)
,rn INT
,sp_text_fin VARCHAR(8000))
DECLARE @nofRows INT = (SELECT COUNT(*) FROM #tbl_sp_no_comments)
DECLARE @ii INT = 1
WHILE (@nofRows >= @ii)
BEGIN
DECLARE @LastLB INT = 0
DECLARE @Com INT = 0
SET @Com = (SELECT CHARINDEX('--', sp_text,@com) FROM #tbl_sp_no_comments WHERE rn = @ii)
SET @LastLB = (SELECT CHARINDEX(CHAR(10), sp_text, @LastLB) FROM #tbl_sp_no_comments WHERE rn = @ii)
INSERT INTO #tbl_sp_no_comments_fin (rn, sp_text_fin)
SELECT
rn
,CASE WHEN @Com = 0 THEN sp_text
WHEN @Com <> 0 THEN SUBSTRING(sp_text, 0, @Com) END as new_sp_text
FROM #tbl_sp_no_comments
WHERE
rn = @ii
SET @ii = @ii + 1
END
DROP TABLE IF EXISTS dbo.Query_results_no_comment
SELECT
rn
,sp_text_fin
INTO dbo.Query_results_no_comment
FROM #tbl_sp_no_comments_fin
WHERE
DATALENGTH(sp_text_fin) > 0
AND LEN(sp_text_fin) > 0
/* ******************************
*
* 3. Create data lineage
*
******************************** */
DECLARE @orig_q VARCHAR(MAX)
SELECT @orig_q = COALESCE(@orig_q + ', ', '') + sp_text_fin
FROM dbo.Query_results_no_comment
order by rn asc
DROP TABLE IF EXISTS dbo.LN_Query
DECLARE @stmt2 NVARCHAR(MAX)
SET @stmt2 = REPLACE(REPLACE(@orig_q, CHAR(13), ' '), CHAR(10), ' ')
SELECT
TRIM(REPLACE(value, ' ','')) as val
,dbo.fn_removelistChars(value) as val_f
,row_number() over (ORDER BY (SELECT 1)) as rn
INTO dbo.LN_Query
from string_split(REPLACE(@stmt2, CHAR(13), ' '), ' ' )
WHERE
REPLACE(value, ' ','') <> ' '
OR REPLACE(value, ' ','') <> ' '
DECLARE @table TABLE (command_ VARCHAR(200), location_ VARCHAR(200), order_ INT)
DECLARE @command_i VARCHAR(200) = ''
DECLARE @next_step BIT = 0 -- FALSE (1 = TRUE)
DECLARE @previous VARCHAR(200) = ''
DECLARE @order INT = 1
DECLARE @previous_cmd VARCHAR(200) = ''
DECLARE @previous_step BIT = 0 -- FALSE
DECLARE @ttok VARCHAR(100) = ''
DECLARE @i_row INT = 1
DECLARE @max_row INT = (SELECT MAX(rn) FROM dbo.LN_Query)
DECLARE @row_commands_1 NVARCHAR(1000) = 'select,delete,insert,drop,create,select,truncate,exec,execute'
DECLARE @row_commands_2 NVARCHAR(1000) = 'select,not,if,exists,select'
DECLARE @row_commands_3 NVARCHAR(1000) = 'from,join,into,table,exists,sys.dm_exec_sql_text,sys.dm_exec_cursors,exec,execute'
WHILE (@max_row >= @i_row)
BEGIN
DECLARE @command VARCHAR(1000) = (SELECT val FROM dbo.LN_Query WHERE rn = @i_row)
IF @command IN (SELECT REPLACE(TRIM(LOWER(value)), ' ','') FROM STRING_SPLIT(@row_commands_1, ','))
BEGIN
IF LOWER(@command) = 'select'
BEGIN
SET @command = 'select'
END
SET @command_i = @command
END
IF (@next_step = 1)
BEGIN
IF @command NOT IN (SELECT REPLACE(TRIM(LOWER(value)), ' ',' ') FROM STRING_SPLIT(@row_commands_2,','))
BEGIN
IF (LOWER(@previous) = 'into')
SET @command_i = 'select into'
IF (@command NOT LIKE '%#%' OR @command NOT LIKE '%#%')
SET @ttok = ' ' + @command + ' as ('
IF (@ttok NOT IN (SELECT @stmt2))
INSERT INTO @table (command_, location_, order_)
SELECT
@command_i
,@command
,@order
SET @command_i = @command_i
END
SET @next_step = 0
IF @command IN ('sys.dm_exec_sql_text','sys.dm_exec_cursors')
BEGIN SET @next_step = 1 END
END
IF (@command IN (SELECT REPLACE(TRIM(LOWER(value)), ' ','') FROM STRING_SPLIT(@row_commands_3,',')))
BEGIN
SET @next_step = 1
END
SET @previous_cmd = @command_i
SET @previous = @command
SET @i_row = @i_row + 1
END
DROP TABLE IF EXISTS dbo.final_result
-- Final results
SELECT *
,row_number() over (order by (select 1)) as rn
INTO dbo.final_result
FROM @table
SELECT
[command_] AS Clause_name
,[location_] AS Object_Name
,rn AS order_DL
FROM dbo.final_result
END;
GO
/* **************************
*
* -- TEST functionalities
*
************************* */
DECLARE @test_query VARCHAR(MAX) = '
-- This is a sample query to test data lineage
SELECT
s.[BusinessEntityID]
,p.[Title]
,p.[FirstName]
,p.[MiddleName]
-- ,p.[LastName]
,p.[Suffix]
,e.[JobTitle] as JobName
,p.[EmailPromotion]
,s.[SalesQuota]
,s.[SalesYTD]
,s.[SalesLastYear]
,( SELECT GETDATE() ) AS DateNow
,( select count(*) FROM [AdventureWorks2014].sales.[SalesPerson] ) as totalSales
/*
Adding some comments!
*/
FROM [AdventureWorks2014].sales.[SalesPerson] s
LEFT JOIN [AdventureWorks2014].[HumanResources].[Employee] e
ON e.[BusinessEntityID] = s.[BusinessEntityID]
INNER JOIN [AdventureWorks2014].[Person].[Person] AS p
ON p.[BusinessEntityID] = s.[BusinessEntityID]
'
EXEC dbo.TSQL_data_lineage
@InputQuery = @test_query