Variables and data types can be defined in a PL/SQL block, which makes the PL/SQL block more flexible with data processing.
The definition of variables and types is placed in the variable Declarations section of the PL/SQL block.
Definition and use of variables
There are two types of definitions for variables, namely:
Variable name TYPE [constraint][default default]
Variable name TYPE [constraint][:= initial value]
The part that is delimited by square brackets is optional.
Constraints are used to specify the conditions that a variable must satisfy, such as a "notnull" constraint that specifies that a variable cannot be null, so that the variable is defined with an initial value or a default value.
Variable names are subject to certain naming conventions.
Variable names must begin with a letter, contain numbers, letters, and underscores to reverse the $, # notation, and cannot exceed 30 characters in length and cannot be the same as the Oracle keyword.
Variable names are not related to capitalization.
The type of a variable can be either a data type provided by PL/SQL or a user-defined type.
Basic Data type description
Binary integer integer type data (note: binary binary)
number[(precision, fractional)] can represent integers and floating-point numbers
CHAR [(maximum length)] string, up to 32 767 bytes in length
Long string
Long raw-to-
VARCHAR2 (maximum length) consistent with VARCHAR2 in the database
DATE and date-in the database
Boolean evaluates to True and False
In addition to the basic types described above, PL/SQL also defines some subtypes .
These basic data types can be used in a PL/SQL block, or their subtypes, but the final type of the data is still its base type.
Child Type Parent Type description
Natural and Naturaln BINARY INTEGER Natural number, where the latter cannot be empty
Positive and Positiven BINARY integer positive integer, where the latter cannot be null
Signtype Binary INTEGER values are 11, 0, I
int and integer BINARY integer integer type data
DEC, DECIMAL NUMERIC BINARY INTEGER equivalent to number, provides ANSI compatibility
FLOAT Binary INTEGER 126-bit binary floating-point number
REAL Binary INTEGER 63-bit binary floating-point number
CHARACTER Char is equivalent to char and provides ANSI compatibility
STRING VARCHAR2 is equivalent to VARCHAR2 and provides ANSI compatibility
VARCHAR VARCHAR2 and VARCHAR2 equivalence
Variables can be defined by specifying a default value or an initial value, which can be assigned during the operation of a PL/SQL block.
The format of the assignment is:
Variable name: = Expression
PL/SQL Programming _ variables