PL SQL Variables
PL SQL Variables
PL/SQL - Variables
Advertisements
In this chapter, we will discuss Variables in Pl/SQL. A variable is nothing but a name given
to a storage area that our programs can manipulate. Each variable in PL/SQL has a specific
data type, which determines the size and the layout of the variable's memory; the range of
values that can be stored within that memory and the set of operations that can be applied
to the variable.
The name of a PL/SQL variable consists of a letter optionally followed by more letters,
numerals, dollar signs, underscores, and number signs and should not exceed 30
characters. By default, variable names are not case-sensitive. You cannot use a reserved
PL/SQL keyword as a variable name.
PL/SQL programming language allows to define various types of variables, such as date
time data types, records, collections, etc. which we will cover in subsequent chapters. For
this chapter, let us study only basic variable types.
Where, variable_name is a valid identifier in PL/SQL, datatype must be a valid PL/SQL data
type or any user defined data type which we already have discussed in the last chapter.
Some valid variable declarations along with their definition are shown below −
https://www.tutorialspoint.com/plsql/plsql_variable_types.htm 1/5
4/2/2019 PL/SQL Variables
name varchar2(25);
address varchar2(100);
When you provide a size, scale or precision limit with the data type, it is called a
constrained declaration. Constrained declarations require less memory than
unconstrained declarations. For example −
For example −
counter binary_integer := 0;
greetings varchar2(20) DEFAULT 'Have a Good Day';
You can also specify that a variable should not have a NULL value using the NOT NULL
constraint. If you use the NOT NULL constraint, you must explicitly assign an initial value
for that variable.
DECLARE
a integer := 10;
b integer := 20;
c integer;
f real;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
f := 70.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;
/
Value of c: 30
Value of f: 23.333333333333333333
https://www.tutorialspoint.com/plsql/plsql_variable_types.htm 2/5
4/2/2019 PL/SQL Variables
Local variables − Variables declared in an inner block and not accessible to outer
blocks.
Following example shows the usage of Local and Global variables in its simple form −
DECLARE
-- Global variables
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
-- Local variables
num1 number := 195;
num2 number := 185;
BEGIN
dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
END;
END;
/
Table Created
The following program assigns values from the above table to PL/SQL variables using the
SELECT INTO clause of SQL −
DECLARE
c_id customers.id%type := 1;
c_name customers.name%type;
c_addr customers.address%type;
c_sal customers.salary%type;
BEGIN
SELECT name, address, salary INTO c_name, c_addr, c_sal
FROM customers
WHERE id = c_id;
dbms_output.put_line
('Customer ' ||c_name || ' from ' || c_addr || ' earns ' || c_sal);
END;
/
https://www.tutorialspoint.com/plsql/plsql_variable_types.htm 4/5
4/2/2019 PL/SQL Variables
Advertisements
https://www.tutorialspoint.com/plsql/plsql_variable_types.htm 5/5