PL/SQL Constants And Literals

Suriya Ravichandran



PL/SQL Constants: Simplifying Code Stability

In PL/SQL, a constant is a fixed value that remains unchanged throughout a program, offering a streamlined way to manage and update specific values. Constants are particularly useful when you want to use a consistent value across multiple parts of your code.

Syntax for Declaring a Constant:

constant_name CONSTANT datatype := VALUE;

  • constant_name: The name assigned to the constant.
  • datatype: The data type of the constant.
  • VALUE: The fixed value assigned to the constant when declared, and it cannot be changed later.

Example:

The simple program that uses a constant to convert temperatures from Celsius to Fahrenheit:



DECLARE
   -- Declare the constant for Celsius to Fahrenheit conversion
   CELSIUS_CONSTANT CONSTANT NUMBER := 9.0 / 5.0;
   
   -- Other variable declarations
   celsius_temperature NUMBER(5,2) := 25.0;
   fahrenheit_temperature NUMBER(5,2);
BEGIN
   -- Processing: Convert Celsius to Fahrenheit
   fahrenheit_temperature := (celsius_temperature * CELSIUS_CONSTANT) + 32.0;
   
   -- Output
   dbms_output.put_line('Celsius Temperature: ' || celsius_temperature);
   dbms_output.put_line('Fahrenheit Temperature: ' || fahrenheit_temperature);
END;



Output:



Celsius Temperature: 25.0
Fahrenheit Temperature: 77.0

PL/SQL procedure successfully completed.


PL/SQL Literals:


In PL/SQL, literals are specific values that are not represented by identifiers. They come in various types, including numeric, character, string, boolean, and date/time literals. These literals are case-sensitive, meaning 'Hello' and 'hello' would be considered different.

Types of PL/SQL Literals:


1.Numeric Literals:
Examples: 123, 4567, 78.9

2.Character Literals:
Examples: 'X', '@', '5', ' ', 'm', '!'

3.String Literals:
Example: 'Greetings, World!'

4.BOOLEAN Literals:
Examples: TRUE, FALSE, NULL

5.Date and Time Literals:
Examples: '15-03-2005', '2023-08-10 18:30:00'






More topic in PL/SQL

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send