"In-line Comment Block" Trick in SQL Server T-SQL

In Transact-SQL (T-SQL) there are two ways to comment SQL code that are similar to comment options in many programming languages. First, there’s the “slash star” (/*) comment block that allows you to comment out multiple rows of SQL. Second, there’s the “dash dash” (- -) in-line comment that comments out all the SQL to the right of dash dash characters on the same line of SQL. While those two comment methods are useful and well known, there is actually a third useful combination that is important to understand.  This other combination lends itself to lots of other useful tips and tricks in T-SQL as well.
The trick is simply this:
The dash dash (- -) comment characters trump the slash star (/*) comment start block characters but do not trump the star slash (*/) end comment block characters. This allows you to remove or add the leading dash dash (- -) comments from/to a slash star (/*) comment block to quickly comment or uncomment the entire block (when the ending */ block is lead by dash dash (- -) characters.)
Image shows how using -- in front of a /* comment block causes the /* to not be effective in commenting out the comment block.
Note that the dash dash (- -) comments out the effectiveness of the slash star (/*) starting comment block and note the dash dash (- -) comment in front of the ending star slash (*/) ending comment block characters. The dash dash does NOT affect the star slash ending comment block characters from ending a slash star comment block.  But while the star slash (*/) is still effective, it doesn’t cause an error in the top example of there being a comment end block without a comment start block.  This next image shows how the dash dash in front of the ending star dash comment ending block is necessary for this trick to function properly.  Without the dash dash in front of the star dash ending block, you’ll receive a compile error.
Shows how not using the dash dash comment in front of the star slash ending comment block causes an error when the starting slash star comment block is commented out by a dash dash.
While this is a multi-line comment method, I call this the “inline comment block” you can turn it on and off in-line in just the first line of the comment block.  This means you can easily use REPLACE ALL to uncomment or comment all of the instances of this block in your script when combined with other identifying characters to distinguish them from the /* comment blocks you always want to leave commented.
Using this method to quickly commenting or uncommenting all of your test SQL in a SQL script is one example of that.
Shows how using typical comment block around test SQL comments out the SQL.
If you wrap all of your test SQL with something like: “/* FOR TESTING ONLY”, you can quickly REPLACE ALL instances of that text with “- -/* FOR TESTING ONLY” to quickly enable all your test statements within your SQL script.  This allows you to quickly add or remove your test SQL from the final compiled T-SQL and not have it effect performance whatsoever when it’s not being used.
Shows how using the dash dash (--) characters in front of the slash star comment block start characters causes the slash star comment block to not be start the comment block.
I use this “in-line comment block” when I need to quickly toggle between commented and uncommented T-SQL or when I need to be able to globally enable blocks of SQL Code that I have commented out.  I find it comes in handy often.   I will follow-up with related tips and tricks that build on this simple concept.

  • The additional slash at the end of each block apparently delineates them so that sql developer recognises them as a separate block. I d rather do without the slashes so sqlplus! What are your thoughts?

Comments are closed.

Let's talk

If you want to get a free consultation without any obligations, fill in the form below and we'll get in touch with you.