Comments are nonexecuting text strings in program code (also known as remarks). Comments can be used to document code or temporarily disable parts of Transact-SQL statements and batches being diagnosed. Using comments to document code makes future program code maintenance easier. Comments are often used to record the program name, the author name, and the dates of major code changes. Comments can be used to describe complicated calculations or explain a programming method.
Microsoft® SQL Server™ supports two types of commenting characters:
- -- (double hyphens). These comment characters can be used on the same line as code to be executed, or on a line by themselves. Everything from the double hyphens to the end of the line is part of the comment. For a multiple-line comment, the double hyphens must appear at the beginning of each comment line. For more information about using the comment characters, see -- (Comment).
- /* ... */ (forward slash-asterisk character pairs). These comment characters can be used on the same line as code to be executed, on lines by themselves, or even within executable code. Everything from the open comment pair (/*) to the close comment pair (*/) is considered part of the comment. For a multiple-line comment, the open-comment character pair (/*) must begin the comment, and the close-comment character pair (*/) must end the comment. No other comment characters should appear on any lines of the comment. For more information about using the /* ... */ comment characters, see /* ... */ (Comment).
Multiple-line /* */ comments cannot span a batch. The complete comment must be contained within a batch. For example, in SQL Query Analyzer and the osql utility, the GO command signals the end of a batch. When the utilities read the characters GO in the first two bytes of a line, they send all the code since the last GO command to the server as one batch. If a GO occurs at the start of a line between the /* and */ delimiters, then an unmatched comment delimiter will be sent with each batch and they will trigger syntax errors. For example, the following script contains syntax errors:
USE Northwind GO SELECT * FROM Employees /* The GO in this comment causes it to be broken in half */ SELECT * FROM Products GO
Here are some valid comments:
USE Northwind GO -- First line of a multiple-line comment. -- Second line of a multiple-line comment. SELECT * FROM Employees GO /* First line of a multiple-line comment. Second line of a multipl-line comment. */ SELECT * FROM Products GO -- Using a comment in a Transact-SQL statement -- during diagnosis. SELECT EmployeeID, /* FirstName, */ LastName FROM Employees -- Using a comment after the code on a line. USE Northwind GO UPDATE Products SET UnitPrice = UnitPrice * .9 -- Try to build market share. GO
Here is some basic information regarding comments:
- All alphanumeric characters or symbols can be used within the comment. SQL Server ignores all characters within a comment, although SQL Query Analyzer, osql, and isql will search for GO as the first two characters in lines within a multiple line comment.
- There is no maximum length for a comment within a batch. A comment can consist of one or more lines.