Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

PL/SQL

Learn step by step



PL/SQL Constant

The Constant is a value in PL/SQL Block which remains unchanged throughout the program. The constant is a literal value define by the user. we can use a constant instead of actual value by declaring it.

For eg., Suppose, we want to write the program in which we offer a discount of 15% for customers, we declare a constant and use it throughout the program. And when we need to change the discount offered, we can change the value of constant which is easier than changing the actual value throughout the program.

Syntax:
constant_name CONSTANT datatype := value;

where, constant_name - is the name of constant (similar to variable_name).
CONSTANT - is the reserved word which ensures that the value doesn't change.
value - it is some value must assigned to the constant. we can't assigned it later.

Example:

We have a table named 'Customers' contains following information :

The following PL/SQL Query will find out the total saving (calculated by constant discount(%)) on a bill for a specified customer (i.e. 2049):

DECLARE
discount constant number(2) := 15;
var_id customers.id%type := 2049;
var_name customers.name%type;
var_purchase customers.purchase%type;
var_discount number(7);
BEGIN
SELECT id, name, purchase INTO var_id, var_name, var_purchase FROM customers WHERE id=var_id;
var_discount := var_purchase * discount / 100;
dbms_output.put_line('Output is : ' || var_name || ' has saved $' || var_discount || ' on this purchase');
END;
/


Related Videos