PL/SQL Variables

Read previous tutorial: PL/SQL Data Types 

In the last tutorial, we learnt few things about variables in PL/SQL. Now, we shall look at the constraint definitions and other related operations.

PL/SQL Variables

Constraints are associated with the variables defined in the code block. A constraint is a condition that is placed on the variable.

PL/SQL Variables

Two frequently used constraints in PL/SQL are:


Constant – This constraint will cause Oracle Engine to ensure the value is not changed after a value is initially assigned to a variable. If a statement tries to change the variable value, an error will be displayed.

Not Null – This constraint will cause Oracle Engine to ensure that the variable always contains a value. If the statement attempts to assign an empty or a null value to that particular variable, the program will be error prone and will get abnormal termination of the program or the exception section will execute, if included in the program code.

Example

How to Assign Values to Variables

A PL/SQL procedure is of no use if there isn’t any method to associate values with the variables. But, PL/SQL provides us with the following ways to achieve this.

:=
This sign includes a ‘colon’ with a succeeding ‘equal to’ sign. This particular sign assigns the parameter on the right hand side of the sign to the parameter or the variable on the left hand side of the sign.

Into
The ‘Into’ keyword is used in a ‘Fetch’ or a ‘Select’ statement. For a Select statement, it assigns the value in the Select section to the variables following the ‘Into’ keyword. When used with the Fetch statement, it assigns the cursor values to the variables that follow the ‘Into’ keyword.

Lets see a program now.


Output
Value of Var3: 90 
Value of Var4: 23.333333333333333333 
PL/SQL procedure successfully completed.

Here, we have declared and defined the 4 variables in the declaration block. The declaration block just holds the variable to be declared in the the current program code. The execution and calculation is done in the BEGIN block. We can access the variables declared in the DECLARE block here.

Scope of Variables

Scope defines the visibility of a particular variable in a program code. PL/SQL gives us the option to develop nesting of blocks which means that every block can contain another block within itself. However, the outer block won’t be accessible.

There are two types of variable scope.

1. Local Variables
2. Global Variables

The next example will help you understand the terms local variables and global variables clearly.


Output
Outer Block Variable num1: 100 
Outer Block Variable num2: 200
Inner Block Variable num1: 300 
Inner Block Variable num2: 400 
PL/SQL procedure successfully completed.

This program properly shows that a BEGIN block can contain other blocks as well and can get the correct output. Here we have inner blocks and outer blocks which have limited scope according to their visibility.

1 thought on “PL/SQL Variables”

Leave a Comment

Your email address will not be published. Required fields are marked *