SQL - Comments
The Comments are added in a SQL statement with the purpose of making the statement easier to understand. It makes the SQL statement more readable and hence easier to update it later. Comments are ignored when the statement is executed. In SQL, there are two ways of putting a comment.
- Single line comment
- Multi-line comment
Note: Please note that comments are not supported in MS Access.
Consider a database table called Employee with the following records:
Single line Comment
It starts with -- and ends with the end of that line. Anything after -- to the end of the line is a single line comment and will be ignored when the statement is executed.
In the example below, two line comments are used. Comments are ignored when SQL code is executed.
-- first line comment SELECT Name, Salary FROM Employee; -- second line comment
This will produce the result as shown below:
It starts with /* and ends with */. Anything between /* and */ is a block comment and will be ignored when SQL code is executed.
In the below SQL statement, multi-line comments are used. When this SQL code is executed these block of comments will be ignored.
/* comment line 1 comment line 2 */ SELECT EmpID, Name, City /* more comments */ FROM Employee;
This result of the above SQL statement will be: