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 frequently used to record the program name, the author name, and the dates of major code changes. Comments can be used to describe complex calculations or explain a programming method.
SQL Server 2005 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 in front of each comment line. For more information about how to use the comment characters, see -- (Comment) (Transact-SQL).
- /* ... */ (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 start the comment, and the close-comment character pair (*/) must end the comment. For more information about how to use the /* ... */ comment characters, see /*...*/ (Comment) (Transact-SQL).
Here is some basic information about comments:
- All alphanumeric characters or symbols can be used within the comment. SQL Server ignores all characters within a comment, although SQL Server Management Studio Code Editor, and sqlcmd 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 contain one or more lines.
The following are some examples of valid comments.
USE AdventureWorks; GO -- Single line comment. SELECT EmployeeID, Title FROM HumanResources.Employee; GO /* First line of a multiple-line comment. Second line of a multiple-line comment. */ SELECT Name, ProductNumber, Color FROM Production.Product; GO -- Using a comment in a Transact-SQL statement -- during diagnosis. SELECT ContactID, /* FirstName, */ LastName FROM Person.Contact; -- Using a comment after the code on a line. USE AdventureWorks; GO UPDATE Production.Product SET ListPrice = ListPrice * .9; -- Reduce price to build market share. GO