In previous article, I have mentioned SQL Server Management Studio (SSMS) Versions but now in this article, I have provided examples to comment query in SQL, which can be useful for you as a database developer to provide comments in query, so other developer can understand why specific query was written and how it is used or why query was used.
So, comments are used in code to understand the piece of code more clearly to explain the sections of the SQL statements, and it can be also be used to prevent the statements of SQL to execute.
You can comment SQL Query in 3 ways
- Inline-comments : Comments inside query
- Multiline-comments: Multiple query lines are commented in this type.
- Single line comments: Single query lines is commented.
Single Line comments
Single line comment in SQL can be done using "--Some text--", basically you need to begin your line with two dashes "–"
--Single line comment-- SELECT * FROM [AdventureWorks2012].[dbo].[DatabaseLog]
Single line comment Syntax is same in MySQL, SQL Server and Oracle database, but in MySQL, you need to keep a space after double dashes, like this "-- this is comment --"
Multi Line Comments
Multi line comments, can be used as inline comments also and syntax remains same for SQL Server, MySQL and Oracle database, it starts with "/*" and follow the comment with an asterisk and forward slash basically ends with "*/".
It is C-style comments.
/*This is multi-line comment which will work in all lines end here */ SELECT * FROM [AdventureWorks2012].[dbo].Employee Where MiddleName = 'E'
You can use inline comments using "--" after the query or even using "/* */" whichever works for you, I have taken example of using two dashes "--" comment after query
SELECT * FROM [AdventureWorks2012].[dbo].Employee Where MiddleName = 'E' -- these are conditions And -- with inline-comments Gender = 'F'
and the above examples works perfectly for me in SQL Server.
Or you can also have inline comments as
SELECT * FROM /* Customers; this is comment */
SQL comment block
SQL Comment block is considered when you write comment within a Transact-SQL statement using multiple-line comments which 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 */.
We have given example of it above here is another example
/* multi line comment another comment */ SELECT * FROM [AdventureWorks2012].[dbo].Employee;
Sql Server comment shortcut
The keyboard shortcut to comment query or text in SQL Server is CTRL + K, CTRL + C. The keyboard shortcut to uncomment query or text is CTRL + K, CTRL + U.
SQL Query Comment in PostgreSQL
Commenting text or query in PostgresSQL is same as SQL Server, you can start with "/*" enter text or query and then end it with "*/".
/* The following is a very * non-trivial SQL code */ SELECT * AS result
Comment in MySQL
Comments in MySQL are similar to other similar database, for single line-comments in MySQL you can use syntax as below
-- this is a comment # this is also a comment
For multi-line comments in MySQL, it can be as below
/* This is multline comment */
That's it, hope this article helps you in understanding comments in SQL and how to use them.
Change Comment Shortcut in SSMS
If you want you can also change comment shortcut in SQL Server management studio (SSMS), by following these steps:
- Go to Tools -> Options -> Keyboard (under Environment)
- In 'Show Commands Containing:', type 'Comment'
- Select "Edit.CommentSelection"
- In 'Press Shortcut keys' input, enter your shortcut key's -> Click 'Assign'
- Click 'Ok' to confirm.
You may also like to read:
SQL Server Management Studio (SSMS) Versions
How to Create database in sql server management studio (Create table and Insert data)
Uniqueidentifier in SQL Server
Free SQL Server Reporting Tools
Best Free SQL Server database hosting
Download and Install AdventureWorks database in SSMS
Query to get all database name in SQL Server?
Validate Email address in SQL Server
Best SQL Server Tools for SQL Developers
What is database management system and it's types
Check if table exists then delete it in SQL Server