In PL/SQL, variables are naming containers used to store data value and then refer to the data simply by naming the container. Before you use a variable in a PL/SQL program, you must declare it.
Each variable in the PL/SQL has a specific data type which defines the size of the variable's memory.
PL/SQL follows the naming rules as follows:
The PL/SQL variable should always be declared in the declaration section or in a package as a global variable. Once you declared a variable, memory will be allocated to the variable's value and location will be identified by the variable name.
To declare a variable, you use a variable name followed by the data type and terminated by a semicolon ( ;). The syntax for declaring a variable is:
To declare a variables, use the following syntax:
Where:
variable_name: The valid identifier in PL/SQL.
datatype: The valid PL/SQL data type with size.
If you specify a size, scale or precision limit with the data type, it is called a constrained declaration.
To declare a variables, use the following syntax:
In PL/SQL, if you do not initialize variable using assignment operator, default value will be NULL. To avoid NULL value, you can initialize a variable with a value, you can do so during the declaration.
To initilize a variable, use the following syntax:
Like any other programing language, PL/SQl also allows you to write nested code or subprogram. If variables declare in subprogram, it will not be accessible to an outer block or main program. There scopes are categorize into two types:
To initilize a variable, use the following syntax: