Javatpoint Logo
Javatpoint Logo

SQL Server Comments

A comment is a programmer-readable explanation or annotation used in the SQL statements that do not affect their execution. It is used to make SQL statements easier to understand for humans. We can include the comment in between any keywords, parameters, or punctuation marks in a statement. During the parsing of SQL code, the SQL Server engine usually ignores them. It is matters a lot in SQL queries like programming languages.

Some examples of using comments are:

  • It can be used to label a section of code to make it easier to understand for others.
  • It also allows us to modify things later when we remove a column shown in the output of a SELECT statement but still keep the name in the query.
  • To check data and performance, disable lines of a WHERE clause.

SQL Server provides three types of comment, which are given below:

  1. Single Line Comment
  2. Multiline Comment
  3. Inline Comment
SQL Server Comments

Comments are mostly used to document the code and provide descriptions of what it does. Let us describe each type of comment in detail with examples.

Single-Line Comments

A comment that starts and ends in a single line is known as a single-line comment. We can add comments with the same SQL statement, nested at the end of the SQL statement or on a separate line. The SQL Server engine does not evaluate the comments. Single line comments are represented by the double dash (- -) symbol.

We can use the single-line comment as below:

Example

The following statement is executed correctly and displayed the employee details:

Executing the statement will give the below output without parsing comment:

SQL Server Comments

The following example is another way to write the single line comment in SQL server:

Multiline Comments

We use multiline comments for documenting the block of SQL statements. It can be span in several lines. Multiline comments are represented by the (/* and */) symbol. Line starting with '/*' is considered the starting point of comment and is terminated when '*/' is encountered. *The SQL Server engine ignores the content written in between these symbols.

We can use the multiline comment as below:

Example

The following statement was executed correctly and returned the employee details:

Executing the statement will give the below output without parsing comment:

SQL Server Comments

Inline Comments

It is an extension of multiline comments, which is used to ignore the part of a line. It is stated between the statements and is enclosed between '/*' and '*/' symbols.

Example

The following statement is executed correctly and returned the selected column from the employee table:

Executing the statement will give the below output where we can see that the SQL Server engine has ignored the performance column:

SQL Server Comments

Conclusion

This article will discuss different types of comments used in the SQL Server. Here we have learned in-depth about single-line comments, multiline comments, and inline comments.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA