SQL Server has two different ways to comment code. You may connect here.
Commenting in SQL Server: Two Approaches
Comments are essential in SQL Server for improving code readability, maintaining documentation, and sharing insights about specific code blocks. SQL Server supports two primary ways to add comments: line comments and block comments. Let’s explore both in detail:
1) Line comments
Line comments begin with two dashes (--
) and continue until the end of the line. Anything after the two dashes on the same line is treated as a comment and will not execute. This type of comment is ideal for short notes or clarifications within your SQL code. It can be used anywhere—on its own line or after a line of code.
Example:
SELECT * FROM Sales.Products -- This table contains product information WHERE ProductID > 10 -- Filters for products with an ID greater than 10
In the example above, the comments explain what the code is doing without interfering with execution. Everything before the --
runs as normal T-SQL code.
2) Block comments
Block comments are more versatile and suitable for longer explanations or detailed documentation. They begin with /*
and end with */
. Everything within these markers is treated as a comment, and it can span multiple lines. This makes block comments particularly useful for temporarily disabling large sections of code or for providing detailed information about the logic.
/* This query retrieves all products from the Sales.Products table and filters the results to include only products with an ID greater than 10. */ SELECT * FROM Sales.Products WHERE ProductID > 10
Block comments can also be nested, making them useful when working with complex scripts. However, it’s important to note that the GO
command is not allowed within comments, as it signals a batch separator and is processed by the SQL Server Management Studio (SSMS).
Additional Notes on Comments
- No Length Limit: SQL Server imposes no limit on the length of comments, making them a flexible tool for documentation.
- Nesting Allowed: Comments can be nested, which helps when disabling a portion of the code that already contains comments.
- Best Practices: Use comments thoughtfully to explain complex logic, assumptions, or decisions in your code.
By combining line and block comments effectively, you can make your SQL scripts more understandable and maintainable, benefiting your team and future development efforts.
Reference: Pinal Dave (https://blog.sqlauthority.com)
12 Comments. Leave new
Does the SQL Server 2005 Management Studio tool interpret the TSQL differently than the SQL Server 2000 Query Analyzer tool? The script contains a block comment like:
/*
(TSQL code)
GO
(more TSQL code)
*/
(even more TSQL code)
Running this script in Query Analyzer connected to a SQL Server 2000 server generates an error. Running this script in Managment Studio connected to a SQL Server 2000 server does not. Both tools were connecting to the same SQL Server 2000 serve. So are both client tools doing some interpretation before sending the code to the server?
Hi,
How to add comments to my table for further reference?
As we aware, in oracle there is a provision to add comments to a table.
COMMENT ON TABLE Employee IS ‘This is a table for Employee.’;
Is there any way to add comments in SQL Sevrer??
@Praveen
You can add comment/Description for a table/column in 2 ways.
1. Through Scripts
2. Through User Interface (UI).
1. Through Scripts:
–This is an Example given in Adventure Works Database.
–This is to add a comment to a table
— Table Name : Department, Schema Name: HumanResources.
— Table Description/Comment is : Lookup table containing the departments within the Adventure Works Cycles company.
EXEC sys.sp_addextendedproperty
@name=N’MS_Description’, @value=N’Lookup table containing the departments within the Adventure Works Cycles company.’ ,
@level0type=N’SCHEMA’,@level0name=N’HumanResources’,
@level1type=N’TABLE’,@level1name=N’Department’
— This is to add a comment to one column
— Column Name: ModifiedDate
— Table Name : Department.
— Schema Name : HumanResource.
–Column Description/Comment: Date and time the record was last updated.
EXEC sys.sp_addextendedproperty
@name=N’MS_Description’, @value=N’Date and time the record was last updated.’ ,
@level0type=N’SCHEMA’,@level0name=N’HumanResources’,
@level1type=N’TABLE’,@level1name=N’Department’,
@level2type=N’COLUMN’,@level2name=N’ModifiedDate’
2: Through UI.
Step1 : Log IN to SQL Server Management Studio.
Step2: Expand Server Name
Step3: Expand Databases
Step4: Expand Database Name
Step5: Expand Tables
To Add Comment to Table
Step6: Right Click Table Name – Click Properties.
Step7: Click Extended Properties.
Step8: To Your right side, Under Name: Write any name, Lets Say MS_Description or any other name you want.
Step9: Click On Browse Button, A pop box will show up.
Step10: Type comment in that box, Click Ok.
To Add Comment to column,
Step6(a) Expand Table Name
Step7(a) Expand Columns
Step8(a) Right Click on one Column – Click Properties
Follow steps 7,8, 9 and 10.
~ IM.
use of nested comments, where to use
Hey can anyone help me in using loops to get some values from a database with stored procedure in SQL SERVER
Can you be more specific?
Why do you want to use a loop?
hi thanx a lot i have learned more and more from ur web sit and ur youtube videos bout i have a small request what will be if i want the product id in the following example equal max not a certain value(10 in the below example) plz replay me via mail
SELECT *
FROM Sales.Products
WHERE ProductID > 10
as the above example
SELECT *
FROM Sales.Products
WHERE ProductID =(select MAX(ProductID) from Sales.Products)
Thank you for your suggestions and hints. I have been searching for detailed explanations at creating my own blog. Thank you!
Nice!
Great resource indeed. I read some interesting answers of some common questions.
This is an incredible rousing article. I am essentially satisfied with your great work. Comments in code are notes readable by people but ignored by the compiler, and allow programmers to provide documentation for informational.