Using Comments

Previous
Previous
Next
Next

The PL/SQL compiler ignores comments, but you should not. Adding comments to your program improves readability and helps others understand your code. Generally, you use comments to describe the purpose and use of each code segment. PL/SQL supports single-line and multiple-line comment styles.

Single-line comments begin with a double hyphen (--) anywhere on a line and extend to the end of the line. Multiple-line comments begin with a slash and an asterisk (/*), end with an asterisk and a slash (*/), and can span multiple lines. See Example: Using Comments in PL/SQL.

Using Comments in PL/SQL

DECLARE  -- Declare variables here.
  monthly_salary         NUMBER(6);  -- This is the monthly salary.
  number_of_days_worked  NUMBER(2);  -- This is the days in one month.
  pay_per_day            NUMBER(6,2); -- Calculate this value.
BEGIN
-- First assign values to the variables.
  monthly_salary := 2290;
  number_of_days_worked := 21;

-- Now calculate the value on the following line.
  pay_per_day := monthly_salary/number_of_days_worked;

-- the following displays output from the PL/SQL block
  DBMS_OUTPUT.PUT_LINE('The pay per day is ' || TO_CHAR(pay_per_day));

EXCEPTION
/* This is a simple example of an exeception handler to trap division by zero. 
   In actual practice, it would be best to check whether a variable is
   zero before using it as a divisor. */
  WHEN ZERO_DIVIDE THEN
      pay_per_day := 0; -- set to 0 if divisor equals 0
END;
/

While testing or debugging a program, you might want to disable a line of code. The following example shows how you can disable a single line by making it a comment:

-- pay_per_day := monthly_salary/number_of_days_worked;

You can use multiple-line comment delimiters to comment out large sections of code.