January 20, 2020 11:15 by
Peter
SQL Comments statement can make your application easier for you to read and maintain. For example, we can include a comment in a statement that describes the purpose of the statement within your application with the exception of hints, comments within SQL. The statement does not affect the statement execution. Please refer to using hints on using this particular form of comment statement.
A comment can appear between any keywords, parameters, or punctuation marks in a statement. You can include a comment in a statement in two ways:
- Begin the comment with a slash and an asterisk (/*). Proceed with the text of the comment. This text can span multiple lines.
- End the comment with an asterisk and a slash (*/). The opening and terminating characters need not be separated from the text by a space or a line break.
- Begin the comment with -- (two hyphens). Proceed with the text of the comment. This text cannot extend to a new line. End the comment with a line break.
Some of the tools used to enter SQL have additional restrictions. For example, if you are using SQL*plus, by default you cannot have a blank line inside a multiline comment.
For more information, please refer to the documentation for the tool you use as an interface to the database. A SQL statement can contain multiple comments of both styles. The text of a comment can contain any printable characters in your database character set.
The comment statement indicates the user-provided text. Comments can be inserted on a separate line, nested at the end of a SQL command line, or within a SQL statement. The server does not evaluate the comment.
SQL Comment uses the two hyphens (--) for single-line or nested comments. Comments inserted with -- are terminated by a new line, which is specified with a carriage return. Character (U+000A), line feed character (U+000D), or a combination of the two in SQL comments.
There is no maximum length for comments. The following table lists the keyboard shortcuts that you can use to comment or uncomment text.
Syntax
-- text_of_comment
Examples
The following example uses the -- commenting characters.
Syntax
-- Choose the sample database.
USE sample;
GO
-- Choose all columns and all rows from the Address table.
SELECT *
FROM OrderDetails
ORDER BY OrderId ASC; -- We do not have to specify ASC because
-- that is the default.
SQL Single Line Comments
Single line comments start with --. Any text between -- and the end of the line will be ignored (will not be executed). The following example uses a single-line comment as an explanation.
Syntax
--Select all:
SELECT * FROM OrderDetails ;
The following example uses a single-line comment to ignore the end of a line.
Syntax
SELECT * FROM OrderDetails -- WHERE OrderName='Coffee';
The following example uses a single-line comment to ignore a statement.
Syntax
--SELECT * FROM OrderDetails;
SELECT * FROM OrderDetails ;
SQL Multi-line Comments
SQL Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored. The following example uses a multi-line comment as an explanation.
Syntax
/*Select all the columns
of all the records
in the OrderDetails table:*/
SELECT * FROM OrderDetails;
The following example uses a multi-line comment to ignore many statements.
Syntax
/*SELECT * FROM Customers;
SELECT * FROM Products;
SELECT * FROM Orders;
SELECT * FROM Categories;*/
SELECT * FROM OrderDetails;
To ignore just a part of a statement, also use the /* */ comment. The following example uses a comment to ignore part of a line.
Syntax
SELECT CustomerName, /*City,*/ Country FROM Customers;
The following example uses a comment to ignore part of a statement
Syntax
SELECT * FROM OrderDetails WHERE (OrderName LIKE 'L%'
OR OrderName LIKE 'R%' /*OR OrderName LIKE 'S%'
OR OrderName LIKE 'T%'*/ OR OrderName LIKE 'W%')
AND OrderName ='Mango'
ORDER BY OrderrAddress;
Summary
In this article, you learned how to use a SQL Comments statement with various options.