Constant - (PL/SQL Tutorial)
In PL/SQL, a constant is an identifier that is assigned a value that cannot be changed during program execution. It is a kind of variable that behaves like a read-only variable. Constants are useful for storing values that are used repeatedly in a program.
Syntax
The syntax for defining a constant in PL/SQL is as follows:
CONSTANT constant_name constant_data_type := constant_value;
Here, constant_name
is the name of the constant, constant_data_type
is the data type of the constant, and constant_value
is the value assigned to the constant.
Example
Here is an example of defining a constant in PL/SQL:
DECLARE
pi CONSTANT NUMBER := 3.14159;
message CONSTANT VARCHAR2(20) := 'Hello, World!';
BEGIN
DBMS_OUTPUT.PUT_LINE(pi);
DBMS_OUTPUT.PUT_LINE(message);
END;
Output
The output of the above program will be:
3.14159
Hello, World!
Explanation
In the above example, we have defined two constants, pi
and message
. pi
has a value of 3.14159
and is of data type NUMBER
, and message
has a value of Hello, World!
and is of data type VARCHAR2
. We have then used the DBMS_OUTPUT.PUT_LINE
procedure to display the values of the constants.
Use
Constants are used to store values that do not change during program execution. They are useful for storing values that are used repeatedly in a program, as they save memory and make programs easier to read and maintain.
Important Points
- A constant is an identifier that is assigned a value that cannot be changed during program execution.
- Constants are useful for storing values that are used repeatedly in a program.
- The syntax for defining a constant is
CONSTANT constant_name constant_data_type := constant_value;
.
Summary
In summary, a constant in PL/SQL is a read-only variable that is assigned a value that cannot be changed during program execution. Constants are useful for storing values that are used repeatedly in a program, as they save memory and make programs easier to read and maintain.