Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

The case against using single-line comments

By Cade Bryant,

This isn't a "script" per se - but rather an observation about a common issue in writing scripts.

I want to alert SQL programmers to the issues surrounding the use of single-line comments.  I try to avoid these whenever possible, and use block comments instead (except, of course, when commenting out the keyword GO).

The reason is that, if a script is copied into a text editor that "wraps" lines, some single-line comments could be broken up into multiple lines.  If one were to copy that code from the editor into Query Analyzer and execute it, the result will be a syntax error, or in the worst case scenario, the execution of dangerous and unwanted code.

I've actually seen this happen with scripts submitted to SQLServerCentral.com.  The text box wherein scripts are pasted on this site "wraps" the text, and I've seen several where single-line comments were broken up into multiple lines.

Below, I have written a couple of examples to illustrate the importance of this:
 

Total article views: 261 | Views in the last 30 days: 1
 
Related Articles
BLOG

Commenting TSQL Scripts

If you have ever searched for “commenting in TSQL” scripts, I am sure that you’ve found hundreds or ...

FORUM

Commenting

How much detail should be used in comments, do you assume a level of understanding? One comment I ...

ARTICLE

Invisible Comments

I ran into a dilemma when I was told that I should not allow potential competitors to view my JavaSc...

ARTICLE

Database Commenting Guideline

Commenting seems to be the bane of every programmer, database or otherwise. Sachin Dedhiya brings us...

ARTICLE

A simple trick for “Block Comment” syntax in SSMS

Make your comment blocks much more useful, in a surprising number of ways

 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones