Introduction
In this tutorial, we will cover the basics of declaring variables in PL/SQL.
Variable Declaration
In PL/SQL, a variable is a storage location that holds a value. To declare a variable, you need to specify its data type and name. The basic syntax for declaring a variable is:
variable_name datatype [NOT NULL := value];
Here is an example of declaring a variable named x
of the data type INTEGER
and assigning it a value of 10
:
DECLARE x INTEGER := 10; BEGIN -- statements END;
You can also declare multiple variables of the same data type in a single statement:
DECLARE x INTEGER := 10; y INTEGER := 20; z INTEGER := 30; BEGIN -- statements END;
Data Types
PL/SQL supports various data types such as:
INTEGER
: It stores whole numbers.
DECLARE x INTEGER := 10; BEGIN -- statements END;
VARCHAR2
: It stores variable-length character strings. The maximum size can be specified in bytes or characters.
DECLARE x VARCHAR2(10) := 'Hello'; BEGIN -- statements END;
DATE
: It stores date and time values.
DECLARE x DATE := SYSDATE; BEGIN -- statements END;
BOOLEAN
: It storesTRUE
,FALSE
orNULL
values.
DECLARE x BOOLEAN := TRUE; BEGIN -- statements END;
Conclusion
In this tutorial, we have covered the basics of declaring variables in PL/SQL. We have discussed the syntax and examples of declaring variables of different data types such as INTEGER
, VARCHAR2
, DATE
and BOOLEAN
. Remember to always specify the data type and assign a value or specify the default value when declaring a variable.
Leave a comment