Thursday, April 22, 2010

Comments in PL/SQL

The PL/SQL compiler ignores comments, but you should not. Adding comments to your program promotes readability and aids understanding. Generally, you use comments to describe the purpose and use of each code segment. PL/SQL supports two comment styles: single-line and multi-line.
In Oracle, comments may be introduced in two ways:


With /*...*/, as in C.

With a line that begins with two dashes --.

Thus:

-- This is a comment

SELECT * /* and so is this */

FROM R;

Comments can be of two types


Single-Line Comments

Single-line comments begin with a double hyphen (--) anywhere on a line and extend to the end of the line. A few examples follow:

-- begin Process
SELECT name INTO employee FROM emp -- get first name
WHERE empno = emp_id;

Notice that comments can appear within a statement at the end of a line.

While testing or debugging a program, you might want to disable a line of code. The following example shows how you can "comment-out" the line:

-- DELETE FROM emp WHERE comm IS NULL;

Multi-line Comments

Multi-line comments begin with a slash-asterisk (/*), end with an asterisk-slash (*/), and can span multiple lines. Some examples follow:



BEGIN

...

/* Compute a 15% bonus for top-rated employees. */

IF rating > 90 THEN

bonus := salary * 0.15 /* bonus is based on salary */

ELSE

bonus := 0;

END IF;

...

/* The following line computes the area of a

circle using pi, which is the ratio between

the circumference and diameter. */

area := pi * radius**2;

END;

Restrictions on Comments

You cannot nest comments. Also, you cannot use single-line comments in a PL/SQL block that will be processed dynamically by an Oracle Precompiler program because end-of-line characters are ignored. As a result, single-line comments extend to the end of the block, not just to the end of a line. So, use multi-line comments instead.