Declaring Variables:
Variables can be declared in several ways. The two examples provided above have shown how to declare variables and assign values to them. Many variables can contain default values, that can be used to initialize values and certain other variables can take constant values. The given example describes the usage of the keywords DEFAULT and CONSTANT.
Using DEFAULT
DECLARE
S NUMBER DEFAULT 10;
BEGIN
DBMS_OUTPUT.PUT_LINE(s);
END
In the above example, the default value of S i.e 10 is printed and the value in the variable s can be altered. This can also be re-assigned with any other numerical value.
Using CONSTANT
Let's consider a condition where variables have to hold constant values; such as for instance pi has to be assigned 3.14.
ExampleDECLARE
Pi CONSTANT REAL:=3.14;
Area NUMBER;
R NUMBER:=&R;
BEGIN
Area:=pi*r**2;
DBMS_OUTPUT.PUT_LINE('THE AREA OF CIRCLE WITH RADIUS '||r||' IS '||area); END;
In the above example, the variable pi is declared as a constant variable whose value cannot modify anywhere inside the program. Re-assigning the value for the similar variable leads to an error.
Using NOT NULL
By assigning an initial value the declarations can impose the NOT NULL constraint as the subsequent example displays:
DECLARE
s VARCHAR2(10) NOT NULL:='RADIANT';
BEGIN
DBMS_OUTPUT.PUT_LINE(s);
END;
The difference among using NOT NULL and DEFAULT is which, default can be assigned to NULL but NOT NULL as the name suggests cannot contain NULL values.