SQL Comments with Example - Single line and Multiline Comments

.
Comments in SQL - Comments and Uncomment in SQL 

comment in SQL with an example



Comment in SQL 
SQL comments are used to explain sections of SQL statements or to prevent the execution of SQL statements.

Syntax of Comments 
The basic Syntax of the comment is given below

--Select all: 

Example of Comments
A basic example of comments is given below.

 --Select all: 
SELECT * FROM Customers;

Types of Comments 
There are two types of comments in SQL.
  • Single line Comments 
  • Multiline Comments 
  • Inline Comments

Single Line Comments 
In SQL single-line comments are used for Comments starting and ending in a single line are considered as single line comments. A single-line comment starting with ‘–-‘ is a comment and will not be executed.

Example of Single-line Comment

Example-1
Consider the below example to understand the single-line comment. 

--Select all: 
SELECT * FROM Student;

Example-2
Ignore the end of the line.

SELECT * FROM Student-- WHERE City='Gorakpur';

Example-3
Ignore statement. 

--SELECT * FROM Student; 
SELECT * FROM College;

Multiline Comment 
In SQL multi-line comments are used for Comments multiple lines of SQL statement text starting /* and ending */ will be ignored. A Multi-line comment starting with /* and ending with */ is a comment and will not be executed.

Example of Multiline comment

Example-1 
Consider the below example to understand the SQL Statement.

/*Select all the columns of all the 
records in the Employee table:*/ 
SELECT * FROM Employee;

Example-2 
To comment on the part of the SQL Statement consider the below example.

SELECT EmployeeName, /*City,*/ Country FROM Employee;

Example-3 

To comment on the part of the statement in between SQL statements consider the below example. 

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;

  • How To Comment a SQL Statements?
Just follow the below steps to comment a SQL Statements.

Just put (--) 2 hyphens at the start of the Statement to comment on the single line.

Example :  

--SELECT * FROM Student; 


  • How to un-comment a SQL Statement?
Just follow the below steps to uncomment SQL Statements.

Just Remove (--) 2 hyphens at the start of the Statement to comment on the single line.

Example:  

--SELECT * FROM Student; This is Commented Statement. 

SELECT * FROM Student; This is an un-commented Statement. 

Hope!!! The above Tutorials on " Comments in SQL With Example "Are helpful for you...

Team,
QA acharya

Post a Comment

0 Comments