SQL Comment

SQL Comment / SQL Server Comment: This SQL tutorial explains how to include comments in sql server, with syntax and examples that can be followed. SQL Comments in code can be used to explain different sections of SQL statements; in that way, it helps you better understand the code. Also, using comments in sql server, we can prevent a certain statement from being executed when necessary.

We have several options for including comments in sql server statements. Because sql server comments can be inserted between any keywords, parameters, or punctuation marks within the statement, there are three methods of doing this. And that is:

  • Single-line comments.
  • Multi-line comments
  • In-Line Comments

We will try the above three ways of adding SQL comments with examples.

SQL Comment 1

SQL Single Line Comments

Comments that begin and end with a single line are called Single line comments. A comment which starts with ‘–‘ is not executed, and the text between — (double hyphen) and the end of the line will be ignored without being run.

Examples of Single Line Comments:

--Display All the Rows of Employees Table:  
SELECT * FROM Employees;  

SELECT * FROM Customers -- WHERE City='London';  
The above condition will ignore statements

SQL Comment Multiple Lines

Multiline comments can also be used as inline remarks, and the same syntax applies to SQL Server, MySQL, and Oracle databases. The beginning of the comment should start with “/*”, followed by an asterisk and a forward slash to close it off – finishing with “*/”.

Example of the SQL Multi Line Comments:

/* When you execute the
below statement, it will fetch all the records of Customers Table */
SELECT * FROM Customers;   

In-Line Comments

Comments included between individual lines of code, not sections like multi-line comments, are inline comments. These comments can be indicated with ‘/*’ and ‘*/’, which enclose the comment text. They are inserted into the program code between the lines they comment on and can be removed by deleting these two characters at the end of a line.

Example Of In Line Comments:

SELECT * FROM tblemp;  
SELECT * FROM /* tblcompany;  
SELECT * FROM tblorders;  
SELECT * FROM */ tbldept;  

Select Ename, /*Eaddress,*/ ECity from Employee;

SQL Comment Indicators

The double hyphen represents the SQL Comment Indicator (–), braces ({ } ), and C-style comment delimiters (/* . . . */). This indicates a comment following an SQL statement.

Here are the examples of SQL Comment Indicators:

SELECT * FROM customer; -- Selects all rows and columns  
SELECT * FROM employee; {Selects all rows and columns}  
SELECT * FROM employee; /*Selects all columns and rows*/copy to the clipboard  

Nested SQL Comment

SQL Server allows for comments to be nested. You can have a block opener (/) within an existing comment, and this will cause SQL Server to treat it as a separate nested comment. This type of comment must also have the corresponding close mark (/); otherwise, you get an error message.

DECLARE @Text AS VARCHAR(20);  
GO  
/*   --Outer Block opened 
SELECT @comment = '/*';  --Nested Block opened 
*/ --Nested Block closed 
*/  --Outer Block Closed
SELECT @@VERSION;  
GO

Conclusion:

This article addresses the topic of comments in SQL, showing three ways to apply them: single-line, multi-line, and inline. We include syntax and practical examples for nested comments and comment indicators.

How To Comment In SQL?

The most common way to comment in SQL is to use the /* and */ symbols. For example, if you wanted to SQL Comment Multiple Lines of code, you could use /* this will not be executed */. If you wanted to add a comment to a line of code, you could use –.

How To Comment Out In SQL Server?

You can add or remove a comment to/from a single line of SQL code, multiple lines next to each other, an entire statement in the language, or several adjacent statements. To place a remark on one line of SQL syntax, you should use two hyphens (–) at the start of it.

How do I comment in SQL Server?

Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored.

How do you comment out multiple lines in SQL?

Multiple-line comments must be indicated by /* and */. A stylistic convention often used for multiple-line comments is to begin the first line with /*, subsequent lines with **, and end with */.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment