2.7. Adding Comments to SQL StatementsComments are nonexecutable words or phrases included in SQL queries to make the queries easier to understand (particularly by other people). Comments are ignored by the SQL engine, but they are very useful to programmers in determining what the statement does, when it was written, who wrote it, and so on. There are two ways of including comments in SQL Server . The first way is by the use of dashes, as shown here: SELECT * -- displays "all" attributes FROM Dependent d -- of the Dependent table WHERE d.age > 5 -- where the age of the dependent is greater than 5. The second way of including comments in Server SQL 2005 is by the use of /*...*/ construction. Following is an example of a commented statement that uses this format: SELECT dname, age /* displays the dependent name and age */ FROM Dependent d /* from the Dependent table */ WHERE d.age > 5 /* where the age of the dependent is greater than 5 */
We wish to encourage the use of comments in writing SQL queries, particularly for complex queries, and when queries will be debugged or enhanced by others. SQL Server also has icons to turn lines into comment lines. For example, if you type in the query as shown in Figure 2-3, and then you wish to make the last line a comment line, highlight the last line and clickthe Make Comment button and the last line will become a comment line. If you wish to remove the comment, clickthe button beside it, the Remove Comment button, and the comment will be removed, turning the line into a regular line.
Figure 2-3. Icons for adding/removing comments![]() |