-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
v2_strip_comments.sql
133 lines (98 loc) · 3.02 KB
/
v2_strip_comments.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
USE Lineage;
GO
--EXEC dbo.sql_sample_procedure
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)
)
INSERT INTO dbo.SQL_query_table
EXEC sp_helptext
@objname = 'sql_sample_procedure'
DECLARE @sp_text varchar(8000) = ''
DECLARE @sp_text_row varchar(8000)
DECLARE @sp_no_comment varchar(8000) = ''
DECLARE @c char(1)
DECLARE @comment_count INT = 0
SELECT @sp_text = @sp_text + CASE
WHEN LEN(@sp_text) > 0 THEN '\n'
ELSE '' END + query_txt
FROM dbo.SQL_query_table
DECLARE @i INT = 1
DECLARE @rowcount INT = (SELECT LEN(@sp_text))
WHILE (@i <= @rowcount)
BEGIN
IF SUBSTRING(@sp_text,@i,2) = '/*'
BEGIN
-- SELECT (SUBSTRING(@sp_text,@i,2)) -- Uncomment or Delete
SELECT @comment_count = @comment_count + 1
END
ELSE IF SUBSTRING(@sp_text,@i,2) = '*/'
BEGIN
SELECT @comment_count = @comment_count - 1
-- SELECT @comment_count -- Uncomment or Delete
END
ELSE IF @comment_count = 0
SELECT @sp_no_comment = @sp_no_comment + SUBSTRING(@sp_text,@i,1)
IF SUBSTRING(@sp_text,@i,2) = '*/'
SELECT @i = @i + 2
ELSE
SELECT @i = @i + 1
END
WHILE (@i <= @rowcount)
BEGIN
IF SUBSTRING(@sp_text,@i,4) = '/*/*'
BEGIN
SELECT @comment_count = @comment_count + 2
END
ELSE IF SUBSTRING(@sp_text,@i,4) = '*/*/'
BEGIN
SELECT @comment_count = @comment_count - 2
END
ELSE IF @comment_count = 0
SELECT @sp_no_comment = @sp_no_comment + SUBSTRING(@sp_text,@i,1)
IF SUBSTRING(@sp_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(@sp_no_comment) > 0)
BEGIN
INSERT INTO #tbl_sp_no_comments (sp_text)
SELECT SUBSTRING( @sp_no_comment, 0, CHARINDEX('\n', @sp_no_comment))
SELECT @sp_no_comment = SUBSTRING(@sp_no_comment, CHARINDEX('\n',@sp_no_comment) + 2, LEN(@sp_no_comment))
END
--SELECT * FROM #tbl_sp_no_comments
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)
PRINT @nofRows
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
--,sp_text
--,@Com AS StartCom
--,@LastLB AS endCom
,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 #tbl_sp_no_comments
SELECT sp_text_fin FROM #tbl_sp_no_comments_fin
WHERE
DATALENGTH(sp_text_fin) > 0 AND LEN(sp_text_fin) > 0