Published on

July 23, 2013

Understanding SQL Server Commenting Techniques

Welcome to another blog post in our SQL Basics series! In today’s post, we will explore the importance of commenting techniques in SQL Server and how they can enhance the readability and maintainability of your code.

When writing queries in the SQL Server query window, it is often helpful to include comments that provide additional information about the code. Comments are lines of text that are ignored by the SQL Server query engine when executing the code. They serve as descriptive notes or warnings for other developers, testers, or team members who may review the code.

Single-Line Comments

To create a single-line comment in SQL Server, simply type two hyphen signs ‘–‘ at the beginning of the line. This instructs SQL Server to ignore everything that follows on the same line. Single-line comments are useful for providing brief explanations or clarifications about specific lines of code.

For example:

--This query joins the Employee and Location tables together.
SELECT * FROM Employee AS em INNER JOIN Location AS lo ON em.LocationID = lo.LocationID

Single-line comments can also be used for testing specific segments of code. By commenting out certain lines, you can isolate and run a particular section of code without modifying the surrounding code.

Multi-Line Comments

In addition to single-line comments, SQL Server also supports multi-line comments. To create a multi-line comment, enclose the code block you want to comment out between ‘/*’ and ‘*/’ delimiters. This technique is particularly useful when you want to disable a large segment of code.

For example:

SELECT * FROM Employee AS em 
/*INNER JOIN Location as lo 
ON em.LocationID = lo.LocationID*/

Using multi-line comments can save you time and effort, especially when you need to comment out or uncomment multiple lines of code. Instead of typing the double hyphen sign for each line, you can simply add the opening and closing delimiters once.

Remember, comments should be used wisely to provide meaningful information about the code’s purpose, who it’s intended for, and any relevant details. They serve as a mini informational ‘readme’ file for anyone reading the code, both now and in the future.

By utilizing commenting techniques effectively, you can improve collaboration, troubleshooting, and code maintenance within your SQL Server projects.

That’s all for today’s post! We hope you found this information helpful in understanding SQL Server commenting techniques. Stay tuned for more articles in our SQL Basics series.

Don’t forget to grab a copy of our book, “SQL Basics,” available in both Paperback (USA) and Kindle (Worldwide). It’s a valuable reference that every SQL developer should have!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.