Constants in PL/SQL

  • A constant is a value used in a PL/SQL block which remains unchanged throughout the program.
  • It can be declared and used instead of actual values.
    For example: Assume if programmer has to write a program which will increase salary of the employee upto 10%. So programmer can declare a constant and use it throughout the program.
Syntax:
Constant_name CONSTANT datatype := value;

Lets take an example to understand the declaration and execution of constants.

Example

DECLARE  
   -- constant declaration  
   pi constant number := 3.14;  
   -- other declarations  
   radius number(6,2);   
   dia number(6,2);   
   circumference number(8, 2);  
   area number (11, 2);  
BEGIN   
   -- processing  
   radius := 3.5;   
   dia := radius * 2;   
   circumference := 2.0 * pi * radius;  
   area := pi * radius * radius;  
   -- output  
   dbms_output.put_line('Radius: ' || radius);  
   dbms_output.put_line('Diameter: ' || dia);  
   dbms_output.put_line('Circumference: ' || circumference);  
   dbms_output.put_line('Area: ' || area);  
END;  
/


Output:
Radius: 3.5
Diameter: 7
Circumference: 21.98  
Area: 38.465