SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL Decommenter Part I


T-SQL Decommenter Part I

Author
Message
Toby Ovod-Everett
Toby Ovod-Everett
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 14
Comments posted to this topic are about the item T-SQL Decommenter Part I
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217844 Visits: 24953
An interesting approach, with quite a lot of time invested in it!

I come at this from a different angle. All of my source code is contained in several local Git repositories & I am able to search that code (usually just .sql and .dtsx files) using third-party search software (I use FileSeek). This software allows for the search of regular expressions, so ignoring words which appear after in-line comments is easy enough. Handling block comments would be more tricky.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
eliassal
eliassal
SSC Eights!
SSC Eights! (936 reputation)SSC Eights! (936 reputation)SSC Eights! (936 reputation)SSC Eights! (936 reputation)SSC Eights! (936 reputation)SSC Eights! (936 reputation)SSC Eights! (936 reputation)SSC Eights! (936 reputation)

Group: General Forum Members
Points: 936 Visits: 40
nice and useful idea.It helps a lot, thanks for your hard work



Lee Linares
Lee Linares
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2307 Visits: 1694
Toby,

Thanks for taking the time to develop and share this code. Very useful.
I have run into an issue that has me stumped. When I run the script against a database that has several large stored procedures the script errors out with errors like these:
Msg 9455, Level 16, State 1, Line 92
XML parsing: line 779, character 109, illegal qualified name character
Msg 9455, Level 16, State 1, Line 1
XML parsing: line 626, character 109, illegal qualified name character
It does not error out on all of them. In fact it executes great against the largest stored procedure. I have looked at and compared the stored procedures that parsed into xml without error and those that failed and nothing jumps out at me.
What I find interesting is that when I found the line referenced in the error it was a '<' in 2 of the stored procs and an ampersand in the other one. But in each case there were other occurrences of '<' and '&' that occurred in previous lines of code in the stored proc.
My workaround was to replace:
CAST(N'<?def --' + NCHAR(13)+NCHAR(10) + [Definition] + NCHAR(13)+NCHAR(10) + N'--?>' AS xml)
With
TRY_CONVERT(XML,N'<?def --' + NCHAR(13)+NCHAR(10) + [Definition] + NCHAR(13)+NCHAR(10) + N'--?>',1)

This keeps the code from failing but of course the definition is not created.
Thanks again.

Lee

Toby Ovod-Everett
Toby Ovod-Everett
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 14
Lee Linares - Friday, November 16, 2018 1:36 PM
Toby,

Thanks for taking the time to develop and share this code. Very useful.
I have run into an issue that has me stumped. When I run the script against a database that has several large stored procedures the script errors out with errors like these:
Msg 9455, Level 16, State 1, Line 92
XML parsing: line 779, character 109, illegal qualified name character
Msg 9455, Level 16, State 1, Line 1
XML parsing: line 626, character 109, illegal qualified name character
It does not error out on all of them. In fact it executes great against the largest stored procedure. I have looked at and compared the stored procedures that parsed into xml without error and those that failed and nothing jumps out at me.
What I find interesting is that when I found the line referenced in the error it was a '<' in 2 of the stored procs and an ampersand in the other one. But in each case there were other occurrences of '<' and '&' that occurred in previous lines of code in the stored proc.
My workaround was to replace:
CAST(N'<?def --' + NCHAR(13)+NCHAR(10) + [Definition] + NCHAR(13)+NCHAR(10) + N'--?>' AS xml)
With
TRY_CONVERT(XML,N'<?def --' + NCHAR(13)+NCHAR(10) + [Definition] + NCHAR(13)+NCHAR(10) + N'--?>',1)

This keeps the code from failing but of course the definition is not created.
Thanks again.

Lee

Hi Lee,

Thanks for bringing this to my attention. I'm guilty here of using a technique without fully validating it! I first noticed the technique in Adam Machanic's sp_whoisactive. http://whoisactive.com/docs/10_commands/ provides an illustration of the approach. In older versions of SSMS, there was no support for "Retain CR/LF on copy or save", so one trick was to embed the SQL in and XML definition. Modern SSMS versions have support for CR/LF retention, but they still have a restriction on the maximum size of the text that can be retrieved, so XML is still a workaround.

There is a thread on this subject at https://sqlblogcasts.com/blogs/martinbell/archive/2009/10/25/How-to-display-long-text-in-SSMS.aspx . Unfortunately, I'm stymied when it comes to a robust solution. I can come up with solutions that escape the problematic sequences (in this case, I believe it to be '?>'), but that requires a human to unescape the escapes after copying out the body. It also falls victim to ambiguity between escaped sequences that match unescaped sequences in the original SQL. Note that whilw experimenting, I discovered the '--' in the closing '--?>' is unnecessary.

Another approach would be to bypass the XML workaround and use SSMSBoost, but that feature isn't available in the Community Edition.

Here is a quick test suite for playing around with approaches:
WITH
Tests AS (
SELECT V.[SQL]
FROM ( VALUES
( N'SELECT *'+NCHAR(13)+NCHAR(10)+
N'FROM <bar> Foo' ),
( N'SELECT *, ''?>'''+NCHAR(13)+NCHAR(10)+
N'FROM <bar> Foo' ),
( N'SELECT *, ''--'''+NCHAR(13)+NCHAR(10)+
N'FROM <bar> Foo' ),
( NULL )
) AS V([SQL])
)
SELECT
[SQL],
TRY_CAST(N'<?def --' + NCHAR(13)+NCHAR(10) + [SQL] +
NCHAR(13)+NCHAR(10) + N'--?>' AS xml) AS ProcessingInstruction,
TRY_CAST(N'<?def --' + NCHAR(13)+NCHAR(10) + REPLACE([SQL], '?>', '? >') +
NCHAR(13)+NCHAR(10) + N'?>' AS xml) AS ProcessingInstructionEscape,
TRY_CAST(N'<!--' + NCHAR(13)+NCHAR(10) + [SQL] +
NCHAR(13)+NCHAR(10) + N'-->' AS xml) AS Comment
FROM Tests;

Lee Linares
Lee Linares
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2307 Visits: 1694
Toby,

Thanks so much for the quick response.
I tried your excellent suggestions and found that the 2nd approach using REPLACE fixed my problem perfectly.
I have found your script to be extremely helpful in a task I am involved in where we have changed naming conventions and I needed to find references in procedures using linked server connections.

Thank you again for sharing your code and for your additional help.

Lee
Toby Ovod-Everett
Toby Ovod-Everett
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 14
Lee Linares - Monday, November 19, 2018 7:36 AM
Toby,

Thanks so much for the quick response.
I tried your excellent suggestions and found that the 2nd approach using REPLACE fixed my problem perfectly.
I have found your script to be extremely helpful in a task I am involved in where we have changed naming conventions and I needed to find references in procedures using linked server connections.

Thank you again for sharing your code and for your additional help.

Lee

Just keep in mind that when the REPLACE resolves the issue, that implies there is a "?>" somewhere in the original source code, and the original source code has now been modified to have a "? >" in it. Furthermore, one cannot assume that all "? >" sequences in the output were originally "?>", since both "?>" and "? >" in the original source code will be represented by "? >" in the XML output!

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search