How to PROPERLY add comments in SQL [Multiple Ways]

Objective To use the comment in SQL

Comments are used to prevent line of a statement from the execution, Comments can make your application easier for you to read and maintain, Comments are used to explain sections of SQL statements,

A comment can appear between any keywords, parameters, or punctuation marks in a statement. You can include a comment in a statement in three ways:

Advertisement
  1. Single line comments.
  2. Multi-line comments
  3. Inline comments

 

How to add Single line comment in SQL

  • The comments which start and end in a single line are considered as single-line comments
  • Single line comments begin 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

 

Syntax of SQL single-line comment

-- single line comment
-- another comment
Lines of code to be executed 

 

Examples of SQL single-line comment

Consider employee table to perform practical example

Emp_idEmp_namestreetcityEmp_contactSalaryDept_id
101jonealthanSurat11111112000010001
102cartinudhnaSurat22222221500020001
103krishajavaVadodara33333333000020001
104dhiruramnagarVadodara88888883600030001
105omalthanSurat77777772200030001
106adivesuNavsari23232323500010001
107annantshivnagarNavsari55555553400010002
108yogialthanSurat89898982500010002
109muskanvesuVadodara99999991800010001
110rudrakashihazira12121213100020001

 

Example 1: Write SQL query with the comment to specify what will be the result of the query and which line of the statement will be executed

--Below query is used to display all employee data
--Resulting record set is having column heading same as the column name 
SELECT        Emp_id, Emp_name, street, city, Emp_contact, Salary, Dept_id
FROM            tblemp
  • In the above example, two single-line comments is given using ‘—‘ two hyphens symbol
  • Below the comment, we have specified SQL select statement query which will be executed and return all employee details

OUTPUT:

How to PROPERLY add comments in SQL [Multiple Ways]

 

How to add Multi line comment in SQL

  • The Comments which starts in one line and ended in different line are considered multi-line comments.
  • SQL multiline comments is starting with ‘/*’ as the starting point of comment and are terminated when ‘*/’ is encountered

 

Syntax of SQL Multi-line comment

/* multi line comment
another comment */
Lines of code to be executed;

 

Example of SQL Multi line comment

Consider employee table to perform practical example

Emp_idEmp_namestreetcityEmp_contactSalaryDept_id
101jonealthanSurat11111112000010001
102cartinudhnaSurat22222221500020001
103krishajavaVadodara33333333000020001
104dhiruramnagarVadodara88888883600030001
105omalthanSurat77777772200030001
106adivesuNavsari23232323500010001
107annantshivnagarNavsari55555553400010002
108yogialthanSurat89898982500010002
109muskanvesuVadodara99999991800010001
110rudrakashihazira12121213100020001

Example 2: Write SQL query with the comment to specify what will be the result of the query and which line of the statement will be executed

/* Select all the columns
of all the records
in the Employee table */
SELECT  Emp_id, Emp_name, street, city, Emp_contact, Salary, Dept_id
FROM   tblemp
  • In the above query, SQL multi-line comment is given on three lines using ‘/* */’
  • Below the comment lines, we specified SQL select query specified which executed and returns all employee records with all column values

OUTPUT:

Advertisement

How to PROPERLY add comments in SQL [Multiple Ways]

 

How to add inline comment in SQL

  • Inline, comments are an extension of SQL multi-line comments
  • SQL Inline comments can be stated in between the statements and are enclosed in between ‘/*’ and ‘*/’
  • To ignore just a part of a statement, we can also use the /* */ SQL multi-line comment which is called as Inline comment

 

Syntax of SQL Inline comment

Lines of code to be /* comment in the code */ executed;

Example of SQL Inline comment

Consider employee table to perform practical example

Emp_idEmp_namestreetcityEmp_contactSalaryDept_id
101jonealthanSurat11111112000010001
102cartinudhnaSurat22222221500020001
103krishajavaVadodara33333333000020001
104dhiruramnagarVadodara88888883600030001
105omalthanSurat77777772200030001
106adivesuNavsari23232323500010001
107annantshivnagarNavsari55555553400010002
108yogialthanSurat89898982500010002
109muskanvesuVadodara99999991800010001
110rudrakashihazira12121213100020001

Example 3: Write SQL query with a comment within the query to not to execute some query code part and merge with uncommenting part

SELECT * FROM tblemp;  
SELECT * FROM /* tblcompany;  
SELECT * FROM tblorders;  
SELECT * FROM */ tbldept;  
  • In the above list of queries, SQL inline comment is used to make comments on some part of the query using /* and */
  • The query part which is in-between /*… */ will not be executed so the only first query will be executed and returns all records of tblemp

OUTPUT:

How to PROPERLY add comments in SQL [Multiple Ways]

 

How to add Nested SQL Comment

We can have nested comments in SQL Server.  if you specify the block open (/*) in existing comments, SQL Server treats it as a nested comment. Therefore, the nested comment should have a corresponding block closed(/*) comment mark. In case you do not specify a nested comment block close mark, you get an error message

 

Example of Nested SQL Comment

DECLARE @Text AS VARCHAR(20);  
GO  
/*   --Outer Block opened 
SELECT @comment = '/*';  --Nested Block opened 
*/ --Nested Block closed 
*/  --Outer Block Closed
SELECT @@VERSION;  
GO
  • In the above query, we have used SQL Nested comment by specifying SQL multi-line comment within the comment
  • We started multi-line comment with outer opening /* and before closing outer comment we have started inner nested comment with /* and closed it in next line with */ symbol

OUTPUT:

How to PROPERLY add comments in SQL [Multiple Ways]

 

SQL Comment Indicators

  • SQL Comment Indicator is indicated according to the given examples
  • It includes the double hyphen ( — ), braces ( { } ), and C-style ( /* . . . */ ) comment delimiters. It also includes the comments after the statement

 

SQL Comment Indicators Examples

Consider employee table to perform practical example

Emp_idEmp_namestreetcityEmp_contactSalaryDept_id
101jonealthanSurat11111112000010001
102cartinudhnaSurat22222221500020001
103krishajavaVadodara33333333000020001
104dhiruramnagarVadodara88888883600030001
105omalthanSurat77777772200030001
106adivesuNavsari23232323500010001
107annantshivnagarNavsari55555553400010002
108yogialthanSurat89898982500010002
109muskanvesuVadodara99999991800010001
110rudrakashihazira12121213100020001
SELECT * FROM tblemp; -- Selects all rows and columns  
SELECT * FROM tblemp; {Selects all rows and columns}  
SELECT * FROM tblemp; /*Selects all columns and rows*/copy to the clipboard 

 

Summary

In this article on SQL Comment, we have explained the use of a comment in SQL with the three-way to apply comment in SQL that is a single line, multi-line, and inline comment with syntax and practical example also covered nested comment and comment indicators with practical examples

 

Read More

SQL Comments

Advertisement

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment