91-9990449935 0120-4256464 |
PL/SQL VariablesA variable is a meaningful name which facilitates a programmer to store data temporarily during the execution of code. It helps you to manipulate data in PL/SQL programs. It is nothing except a name given to a storage area. Each variable in the PL/SQL has a specific data type which defines the size and layout of the variable's memory. A variable should not exceed 30 characters. Its letter optionally followed by more letters, dollar signs, numerals, underscore etc. 1. It needs to declare the variable first in the declaration section of a PL/SQL block before using it.2. By default, variable names are not case sensitive. A reserved PL/SQL keyword cannot be used as a variable name.How to declare variable in PL/SQLYou must declare the PL/SQL variable in the declaration section or in a package as a global variable. After the declaration, PL/SQL allocates memory for the variable?s value and the storage location is identified by the variable name. Syntax for declaring variable: Following is the syntax for declaring variable: Here, variable_name is a valid identifier in PL/SQL and datatype must be valid PL/SQL data type. A data type with size, scale or precision limit is called a constrained declaration. The constrained declaration needs less memory than unconstrained declaration. Naming rules for PL/SQL variablesThe variable in PL/SQL must follow some naming rules like other programming languages.
Initializing Variables in PL/SQLEvertime you declare a variable, PL/SQL defines a default value NULL to it. If you want to initialize a variable with other value than NULL value, you can do so during the declaration, by using any one of the following methods.
You can also specify NOT NULL constraint to avoid NULL value. If you specify the NOT NULL constraint, you must assign an initial value for that variable. You must have a good programming skill to initialize variable properly otherwise, sometimes program would produce unexpected result. Example of initilizing variableLet's take a simple example to explain it well: After the execution, this will produce the following result: Variable Scope in PL/SQL:PL/SQL allows nesting of blocks. A program block can contain another inner block. If you declare a variable within an inner block, it is not accessible to an outer block. There are two types of variable scope:
Example of Local and Global variablesLet's take an example to show the usage of Local and Global variables in its simple form: After the execution, this will produce the following result:
Next TopicPL/SQL constants
|