-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
remove_comments.sql
156 lines (115 loc) · 3.22 KB
/
remove_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
USE QL;
GO
GO
CREATE OR ALTER PROCEDURE dbo.remove_comments
/*
Author: Tomaz Kastrun
Date: July 2021
GitHub: github.com/tomaztk
Blogpost: https://tomaztsql.wordpress.com/2021/07/13/remove-comments-from-your-t-sql-code/
Description:
Removing all comments from your T-SQL Query for a given procedure
for better code visibility and readability
Usage:
EXEC dbo.remove_comments
@procedure_name = N'sql_sample_procedure'
*/
(
@Procedure_name VARCHAR(300)
)
AS
BEGIN
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 = @Procedure_name
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
SELECT
rn
,sp_text_fin
FROM #tbl_sp_no_comments_fin
WHERE
DATALENGTH(sp_text_fin) > 0
AND LEN(sp_text_fin) > 0
END;