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.)
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.
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.
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.
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?