SQL SERVER – Two Different Ways to Comment Code – Explanation and Example

SQL Server has two different ways to comment code.

1) Line comments

Line comments starts with two dashes(–) and ends with carriage returns. Everything between two dashes(–) and carriage returns is considered as comment. This comments can be placed anywhere in the code. This comment can be placed after code in the same line and everything before two dashes (–) will execute like normal T-SQL and after two dashes(–) will not be executed and considered as comment.

Example:
SELECT *
–---This table name
FROM Sales.Products
WHERE ProductID > 10 –---This is filter condition

2) Block comments

Block comments starts with a forward-slash and asterisk (/*) and ends with asterisk and forward-slash (*/). Everything between forward-slash and asterisk (/*) and ends with asterisk and forward-slash (*/) is considered as comments. This comment are usually placed before or after big chunk of code, however it can be placed anywhere in the code. This comment can span more than one line.

Example:

/*This is example of blog comment
Following table is example of same retrieve
and filter condition*/
SELECT *
FROM Sales.Products
WHERE ProductID > 10

Additional Notes:
There is no limit for the length of the comment.
Comments can be nested.
GO Command is not allowed anywhere in commented text.

Reference : Pinal Dave (http://blog.SQLAuthority.com)

About these ads