Options!
When you’re writing code, you have some options when it comes to leaving comments:
- Double dashes at the start of a line:
-- Comment!
- Block quotes around a section of text:
/* Comment! */
- No comment whatsoever:
The problem with the double-dash method is that there’s no terminator for it. If you end up pasting the query from a DMV where it’s not all nicely formatted, it’s pretty likely going to end up all on a single line.
With a long enough query, it can be a real pain to scroll across looking for comments and separating them to a new line so you can run the query.
What really sucks is that tools that can automatically format T-SQL for you can’t handle this either. If I could just right click and solve the problem, I’d be talking about that instead.
So, please: If you’re going to put comments in your code, use the block quotes.
Thanks for reading!
Going Further
If this is the kind of SQL Server stuff you love learning about, you’ll love my training. I’m offering a 75% discount to my blog readers if you click from here. I’m also available for consulting if you just don’t have time for that and need to solve performance problems quickly.
Revealing! Never thought ’bout that Thank you for sharing!
Hmmm, I’d never thought of it that way. Brilliant!
I never thought about this. I dont think im ever going to comment using double dashes again.
Holy cow!
This is the best T-SQL hack after `SELECT TOP 0 * INTO new_table FROM old_table`
I was once told that comments without a terminator (double-dashes) cause SQL Server to perform extra work when interacting with the code – however miniscule – vs. block quotes. Have you heard that before? Is there a way to prove that out?
No, I’d never heard that.
You could always hit ctrl-f and do a search for the double-hyphen string. That’s not too much overhead.
Maybe if there’s just one, but if there’s a lot of comments and old code quoted out, it can become time consuming.