The SQL Comments
SQL comments are used to describe SQL sections or codes or to stop the execution of SQL commands, and comments are ignored by the database engine.
In SQL support, there are two types of comments.
- Single-line comments
- Multi-line comments
Here, we will learn about both types with examples.
Examples of SQL Commands
- Single-Line Comments with an Example:
Single-line comments are used for adding comments on a single line of code. In SQL, single-line comments start with “–” (two hyphens) and extend to the end of the line.
–Single-line comment in SQL.
SQL> Select * from Products;
SQL> SELECT * from Products — Where ProductName=’Yippee’;
To ignore a statement of single-line comment.
–SELECT * FROM Products;
SQL> Select * from Products;
In this example, the text after “–” is a comment, and it does not affect the execution of the SQL query.
- Multi-Line Comments with an Example:
In SQL, To add comments that cross numerous lines of code, utilize multi-line comments. Multi-line comments are enclosed in /* */ (forward slash and asterisk)
characters.
/*
This is a multi-line comment in SQL.
It can span multiple lines and is enclosed within /* and */ characters.
*/
SQL> SELECT * FROM Employees;
SQL> SELECT EmployeeName, Salary
FROM Employees;
To just ignore a part of a statement,
SQL> SELECT EmployeeName, /Salary,/ EmployeeId FROM Employees;
In this example, the text enclosed within /* and */ is considered a comment and is ignored when the SQL query is executed. Multi-line comments are helpful for providing detailed explanations or commenting out entire sections of code temporarily.