SQL Comments Statement

SQL Comments statement

 
SQL Comments statement can make your application easier for you to read and maintain. For example, we can include a comment in a statement that describes the purpose of the statement within your application with the exception of hints, comments within SQL. The statement does not affect the statement execution. Please refer to using hints on using this particular form of comment statement.  
 
A comment can appear between any keywords, parameters, or punctuation marks in a statement. You can include a comment in a statement in two ways:
  • Begin the comment with a slash and an asterisk (/*). Proceed with the text of the comment. This text can span multiple lines.
  • End the comment with an asterisk and a slash (*/). The opening and terminating characters need not be separated from the text by a space or a line break.
  • Begin the comment with -- (two hyphens). Proceed with the text of the comment. This text cannot extend to a new line. End the comment with a line break.
Some of the tools used to enter SQL have additional restrictions. For example, if you are using SQL*plus, by default you cannot have a blank line inside a multiline comment.
 
For more information, please refer to the documentation for the tool you use as an interface to the database. A SQL statement can contain multiple comments of both styles. The text of a comment can contain any printable characters in your database character set.
 
The comment statement indicates the user-provided text. Comments can be inserted on a separate line, nested at the end of a SQL command line, or within a SQL statement. The server does not evaluate the comment.  
SQL Comment uses the two hyphens (--) for single-line or nested comments. Comments inserted with -- are terminated by a new line, which is specified with a carriage return. Character (U+000A), line feed character (U+000D), or a combination of the two in SQL comments.
 
There is no maximum length for comments. The following table lists the keyboard shortcuts that you can use to comment or uncomment text.
 
Syntax 
  1. -- text_of_comment     
Examples
 
The following example uses the -- commenting characters.
 
Syntax 
  1. -- Choose the sample database.      
  2. USE sample;      
  3. GO      
  4. -- Choose all columns and all rows from the Address table.      
  5. SELECT *      
  6. FROM OrderDetails      
  7. ORDER BY OrderId  ASC-- We do not have to specify ASC because       
  8. -- that is the default.      

SQL Single Line Comments

 
Single line comments start with --. Any text between -- and the end of the line will be ignored (will not be executed). The following example uses a single-line comment as an explanation.
 
Syntax  
  1. --Select all:    
  2. SELECT * FROM OrderDetails ;  
The following example uses a single-line comment to ignore the end of a line.
 
Syntax 
  1. SELECT * FROM OrderDetails -- WHERE OrderName='Coffee';   
The following example uses a single-line comment to ignore a statement.
 
Syntax 
  1. --SELECT * FROM OrderDetails;    
  2. SELECT * FROM OrderDetails ;   

    SQL Multi-line Comments

     
    SQL Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored. The following example uses a multi-line comment as an explanation.
     
    Syntax  
    1. /*Select all the columns    
    2. of all the records    
    3. in the OrderDetails table:*/    
    4. SELECT * FROM OrderDetails;   
       The following example uses a multi-line comment to ignore many statements.
       
      Syntax 
      1. /*SELECT * FROM Customers;    
      2. SELECT * FROM Products;    
      3. SELECT * FROM Orders;    
      4. SELECT * FROM Categories;*/    
      5. SELECT * FROM OrderDetails;   
        To ignore just a part of a statement, also use the /* */ comment. The following example uses a comment to ignore part of a line.
         
        Syntax 
        1. SELECT CustomerName, /*City,*/ Country FROM Customers;   
        The following example uses a comment to ignore part of a statement
         
        Syntax 
        1. SELECT * FROM OrderDetails WHERE (OrderName LIKE 'L%'    
        2. OR OrderName LIKE 'R%' /*OR OrderName  LIKE 'S%'    
        3. OR OrderName LIKE 'T%'*/ OR OrderName LIKE 'W%')    
        4. AND OrderName ='Mango'    
        5. ORDER BY OrderrAddress;   

          Summary

           
          In this article, you learned how to use a SQL Comments statement with various options.