Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PL/SQL Comments

Summary: In this tutorial, you will learn how to use PL/SQL comments, including single-line and multi-line comments, to improve the readability of your code.

Introduction to PL/SQL Comments #

PL/SQL comments allow you to describe the purpose of a line or a block of PL/SQL code.

When compiling the PL/SQL code, the Oracle precompiler ignores comments. However, you should always use comments to help you and other developers understand it quickly later.

PL/SQL has types of comments:

  • Single-line comments
  • Multi-line comments

Single-line comments #

A single-line comment starts with a double hyphen ( --) that can appear anywhere on a line and extend to the end of the line.

For example, the following single-line comment explains the meaning of the co_vat_rate constant:

-- valued added tax 10%
DECLARE co_vat_rate CONSTANT NUMBER := 0.1; Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

Sometimes, while testing a program, you may use a single-line comment to disable a line of code. For example:

-- UPDATE products SET list_price = 0 WHERE product_id = l_id;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

You can also use the single-line comment if you want to comment just a portion of a line.

The following example shows how to comment out the WHERE clause of the UPDATE statement. All the code that follows the double-hyphen -- to the rest of the line will be treated as a comment:

UPDATE products 
SET list_price = 0; 
-- WHERE product_id = l_id;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

Note that we add the semicolon (;) before the (--) to make the statement valid.

Multi-line comments #

A multi-line comment starts with a forward-slash and asterisk ( /* ) and ends with an asterisk and forward-slash ( */ ), and can span multiple lines:

/*
  This is a multi-line comment
  that can span multiple lines
*/
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

It is possible to use a multi-line comment as a single-line comment:

/* A multi-line comment can be used as a single-line comment */
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

We often use a multi-line comment to describe the purpose of a block of code like the following example:

/*
    This code allow users to enter the customer id and 
    return the corresponding customer name and credit limit
*/
DECLARE
    l_customer_name customers.name%TYPE;
    l_credit_limit customers.credit_limit%TYPE;
BEGIN
   ...
END;
/
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

For maintainability, it is not a good practice to mix comments as follows:

BEGIN
    -- single-line comment /* another comment */
    NULL;
    /* 
        multi-line comment 
        -- that has another single-line comment 
    */
END;
/
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

Instead, use the following:

BEGIN
    -- single-line comment, another comment
    NULL;
    /* 
        multi-line comment 
        that has another single-line comment 
    */
END;
/Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

PL/SQL comment usage notes #

PL/SQL does not allow you to nest a multi-line comment within another multi-line comment. The following code block is not valid:

BEGIN
    /* 
        a multi-line comment 
        /*
            a nested multi-line comment
        */
    */ -- -> error
END;
/
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

You cannot use single-line comments for a PL/SQL block that will be processed dynamically. Since the Oracle precompiler will ignore the end-of-line characters that cause the single-line comments to extend to the end of the block. In this case, you can use multi-line comments instead.

Summary #

  • Use comments to explain code, making it more understandable.
  • Single-line comments start with two hyphens (–). All text from — to the end of line are is ignored.
  • Multi-line comments enclose text between /* and */.
  • Use comments to explain why a piece of code does something, not just what it does.
Was this tutorial helpful?