SQL Comments
SQL comments are used to add
explanatory notes or documentation within the SQL code. Comments are not
executed by the database and do not affect the query or stored procedure logic.
There are two commonly used types of
comments in SQL:
1. Single-line comments: These are used to add
comments that span a single line. In most SQL databases, single-line comments
start with two hyphens (--) and continue until the end of the line. For
example:
-- This is a single-line
comment in SQL
SELECT * FROM users; -- This is another comment
2. Multi-line comments: These are used to add comments
that span multiple lines. In most SQL databases, multi-line comments start with
/* and end with */. For example:
/*
This is a multi-line
comment
in SQL. It can span
multiple lines.
*/
SELECT * FROM orders;
It's important to note that comments
are purely for human readability and are ignored by the database engine when
executing the SQL code. They can be used to provide explanations,
documentations, or temporary code removal without affecting the functionality
of the SQL code.
Example
--Select all:
SELECT * FROM Customers;
The following example uses a single-line comment to ignore the end of a line:
Example
SELECT *
FROM Customers
-- WHERE City='Berlin';
The following example uses a single-line comment to ignore a statement:
Example
--SELECT * FROM Customers;
SELECT * FROM Products;
Example
/*Select all the columns
of all the records
in the Customers table:*/
SELECT * FROM Customers;
The following example uses a multi-line comment to ignore many statements:
Example
/*SELECT * FROM Customers;
SELECT * FROM Products;
SELECT * FROM Orders;
SELECT * FROM
Categories;*/
SELECT * FROM Suppliers;
To
ignore just a part of a statement, also use the /* */ comment.
The following example uses a comment to ignore part of a line:
Example
SELECT CustomerName, /*City,*/ Country
FROM Customers;
The following example uses a comment to ignore part of a statement:
Example
SELECT *
FROM Customers WHERE (CustomerName LIKE 'L%'
OR CustomerName LIKE 'R%'
/*OR CustomerName LIKE 'S%'
OR CustomerName LIKE
'T%'*/ OR CustomerName LIKE 'W%')
AND Country='USA'ORDER BY CustomerName;
0 Comments