Oracle PL/SQL - Initial Values of Variables and Constants

Introduction

In a variable declaration, the initial value is optional unless you specify the NOT NULL constraint.

In a constant declaration, the initial value is required.

To set the initial value, use either the assignment operator := or the keyword DEFAULT, followed by an expression.

The expression can include previously declared constants and previously initialized variables.

The following code assigns initial values to the constant and variables.

The initial value of area depends on the previously declared constant pi and the previously initialized variable radius.

Demo

SQL>
SQL> DECLARE-- w ww.  j  av  a  2  s .c  o  m
  2    hours_worked    INTEGER := 4987;
  3    employee_count  INTEGER := 0;
  4    pi     CONSTANT REAL := 3.14159;
  5    radius          REAL := 1;
  6    area            REAL := (pi * radius**2);
  7  BEGIN
  8    DBMS_OUTPUT.PUT_LINE(hours_worked);
  9    DBMS_OUTPUT.PUT_LINE(employee_count);
 10    DBMS_OUTPUT.PUT_LINE(pi);
 11    DBMS_OUTPUT.PUT_LINE(radius);
 12    DBMS_OUTPUT.PUT_LINE(area);
 13    NULL;
 14  END;
 15  /
4987
0
3.14159
1
3.14159

PL/SQL procedure successfully completed.

SQL>

If you do not specify an initial value for a variable, assign a value to it before using it.

Related Topic