Comments


Comments have two entirely different purposes within a T-SQL batch or stored procedure. First, they can be used to document code, to make it easier for folks who have to maintain software in the future. Second, they can be used to temporarily disable lines of code within your batch when you're trying to get it working.

If you've ever had a programming class, or ever read a programming book, the teacher or author has probably emphasized the use of comments over and over again. It bears repeating.

Using comments is the most reliable way of ensuring that you or anyone else can figure out what your code does. Comments are the most important part of the legacy you leave behind as a programmer. If you get hit by a truck, and don't leave any (or enough) comments in your code, it is probably going to be faster for someone to rewrite all of your code than to try and decipher what made perfect sense to you at the time.

SQL Server has two methods for putting comments in your code. The first is to start the comment with a double dash ( -- ). The double dash can appear anywhere on the line, and anything between the double dash and the end of the line is a comment and will not be executed. For example:

 --this is a comment on the whole line SET @i = 42 --this is a comment, but the preceding code will execute --Nothing on this line executes SET @i = 21 

The other style of comment, which is not seen as often anymore, is the slash-star comment. It works like this:

 /* This is a multi-line, slash-star style comment. note that this line is also part of the comment. This type of comment can end in the middle of a line */ SET @i = 42 

In that case, the SET statement would be executed because it's outside the comment. One thing to watch for is that the string " GO " within a comment on a line by itself causes an error. So don't use the string " GO " in the comment. The more common convention by far is to use the double-dash style comment, and the new Query Analyzer for SQL Server 2000 provides you with an easy tool to create multi-line comments quickly and easily. Just highlight the lines you want to comment and press Control+Shift+C. This adds a double-dash to the beginning of each highlighted line. To uncomment the text, just use Control+Shift+R. This is a quick, easy, and painless way to comment out large chunks of code for testing and put them back later. There aren't any restrictions on any special words in the double-dash comment.

That's how you make a bunch of statements look like none: by commenting statements out. But how can you make a bunch of statements look like one statement?



MCSE Training Guide (70-229). Designing and Implementing Databases with MicrosoftR SQL ServerT 2000 Enterprise Edition
MCSE Training Guide (70-229). Designing and Implementing Databases with MicrosoftR SQL ServerT 2000 Enterprise Edition
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 228

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net