Comments in SQL are utilized either to elucidate sections of SQL statements or to hinder the execution of SQL statements.
Please note that Microsoft Access databases do not support comments. |
Single line comments in SQL begin with — .
Any text following — until the end of the line will be disregarded and not executed.
Below is an example utilizing a single-line comment for clarification:
— Select all: SELECT * FROM Customers; |
In the following example, a single-line comment is employed to disregard the remainder of a line:
SELECT * FROM Customers — WHERE City=’Berlin’;
|
In the following example, a single-line comment is used to bypass a statement:
— SELECT * FROM Customers; SELECT * FROM Products; |
Multi-line comments in SQL commence with /* and conclude with */.
Any text enclosed between /* and */ will be disregarded.
Below is an example employing a multi-line comment for clarification:
/*Select all the columns of all the records in the Customers table:*/ SELECT * FROM Customers; |
In the following example, a multi-line comment is utilized to exclude multiple statements:
/*SELECT * FROM Customers; SELECT * FROM Products; SELECT * FROM Orders; SELECT * FROM Categories;*/ SELECT * FROM Suppliers; |
To disregard only a portion of a statement, employ the /* */ comment.
The subsequent example demonstrates the use of a comment to ignore part of a line:
SELECT CustomerName, /*City,*/ Country FROM Customers; |
In the following example, a comment is utilized to disregard a portion of a statement:
SELECT * FROM Customers WHERE (CustomerName LIKE ‘L%’ OR CustomerName LIKE ‘R%’ /*OR CustomerName LIKE ‘S%’ OR CustomerName LIKE ‘T%’*/ OR CustomerName LIKE ‘W%’) AND Country=‘USA’ ORDER BY CustomerName; |