How to PROPERLY add comments in SQL [Multiple Ways]


SQL

Reviewer: Deepak Prasad

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:

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

 

Sample Employee Table

We will use the following Employee Table through out this article:

Emp_id Emp_name street city Emp_contact Salary Dept_id
101 jone althan Surat 1111111 20000 10001
102 cartin udhna Surat 2222222 15000 20001
103 krish ajava Vadodara 3333333 30000 20001
104 dhiru ramnagar Vadodara 8888888 36000 30001
105 om althan Surat 7777777 22000 30001
106 adi vesu Navsari 2323232 35000 10001
107 annant shivnagar Navsari 5555555 34000 10002
108 yogi althan Surat 8989898 25000 10002
109 muskan vesu Vadodara 9999999 18000 10001
110 rudra kashi hazira 1212121 31000 20001

 

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

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

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:

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

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

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

 

Falguni Thakker

Falguni Thakker

She is a dedicated professional with expertise in SQL, Python, C++, and Linux. Currently serving as a professor at a prestigious university. With a passion for teaching and a strong technical background, she inspires the next generation of computer scientists. You can connect with her on her LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

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 send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment