Comments within SQL
SQL Server: Comments within SQL
How to use comments within your SQL statements in SQL Server (Transact-SQL) with syntax and examples.
Comments can appear on a single line or span across multiple lines.
Syntax
Below are two syntaxes that you can use to create a comment within your SQL statement in SQL Server (Transact-SQL).
Syntax Using -- comment text
The syntax for creating a SQL comment using the -- symbol in SQL Server (Transact-SQL) is:
-- comment goes here
In SQL Server, a comment started with -- symbol must be at the end of a line in your SQL statement with a line break after it. This method of commenting can only span a single line within your SQL and must be at the end of the line.
Syntax Using /* and */ symbols
The syntax for creating a SQL comment using /* and */ symbols in SQL Server (Transact-SQL) is:
/* comment goes here */
In SQL Server, a comment that starts with /* symbol and ends with */ and can be anywhere in your SQL statement. This method of commenting can span several lines within your SQL.
Example - Comment on a Single Line
You can create a SQL comment on a single line in your SQL statement in SQL Server (Transact-SQL).
Let's look at a SQL comment example that shows a SQL comment on its own line:
SELECT name as username, create_date
/* Hello World creation time */
FROM sys.database_principals;
Here is a SQL comment that appears in the middle of the line:
SELECT /* Hello World creation time */ name as username, create_date
FROM sys.database_principals;
Here is a SQL comment that appears at the end of the line:
SELECT name as username, create_date /* Hello World creation time */
FROM sys.database_principals;
or
SELECT name as username, create_date -- Hello World creation time
FROM sys.database_principals;
Example - Comment on Multiple Lines
In SQL Server (Transact-SQL), you can create a SQL comment that spans multiple lines in your SQL statement. For example:
SELECT name as username, create_date
/*
* Hello World creation time
* Purpose: To show a comment that spans multiple lines in your SQL statement.
*/
FROM sys.database_principals;
This SQL comment spans across multiple lines in SQL Server - in this example, it spans across 4 lines.
In SQL Server, you can also create a SQL comment that spans multiple lines using this syntax:
SELECT name as username, create_date /* Hello World creation time
Now show a comment that spans
multiple lines in your SQL statement. */
FROM sys.database_principals;
SQL Server (Transact-SQL) will assume that everything after the /* symbol is a comment until it reaches the */ symbol, even if it spans multiple lines within the SQL statement. So in this example, the SQL comment will span across 3 lines.