PL/SQL Constants:
A constant holds a value used in a PL/SQL block that does not change throughout the program. It is a user-defined literal value.
Syntax to declare a constant:
constant_name CONSTANT datatype := VALUE;
Where:
constant_name – is a valid identifier name.
CONSTANT – is a keyword.
VALUE – is a value which must be assigned to a constant when it is declared. You cannot assign a value later.
Example:
DECLARE
-- constant declaration
pi constant number := 3.141592654;
-- other declarations
radius number(5,2);
dia number(5,2);
circumference number(7, 2);
area number (10, 2);
BEGIN
-- processing
radius := 10.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: 10.5
Diameter: 21
Circumference: 65.97
Area: 346.36
PL/SQL Literals:
Literals is an explicit numeric, character, string or Boolean values which are not represented by identifiers i.e. TRUE, NULL, w3spoint etc.
Note: PL/SQL literals are case-sensitive.
Types of literals in PL/SQL:
1. Numeric Literals (765, 23.56 etc.).
2. Character Literals (‘A’ ‘%’ ‘9’ ‘ ‘ ‘z’ etc.).
3. String Literals (tutorialspointexamples.com etc.).
4. BOOLEAN Literals (TRUE, FALSE and NULL).
5. Date and Time Literals (‘2016-12-25’ ‘2016-02-03 12:10:01’ etc.).