Nested If: Condition in a condition is called nested (IF under IF)
Syntax: If Then
satement-1;
If Condition then
satement-2;
else
statement-3;
else
statement-4;
end if;
Example: Program to calculate bonus based on the anual salary and job
Task : Develop a program to calculate the electricity amount based on on total units and types of meter
Sample Data:
MNO SMR EMR MTYPE NU TOTAMT REMARKS
-----------------------------------------------------------------------
801 100 250 D
802 245 500 D
900 100 275 N
901 450 800 N
902 200 455 X INVALID TYPE
----------------------------------------------------------------------
Read a mno from the table electricity
Calculate nu
calculate totamt based on nu and mtype
1. If mtype is 'D' (Domestic) and nu< 100 then Rs 1/- per unit
2. If mtype is 'd' and nu between 100 and 200 then Rs 2/- per unit
3. if mtype is 'd' and nu between 200 and 350 then Rs 3/- per unit
4. if mtype is 'd' and nu > 350 then Rs 5/- unit
1. If mtype is 'n' (Non domestic) and nu< 100 then Rs 3/- per unit
2. If mtype is 'n' and nu between 100 and 200 then Rs 5/- per unit
3. if mtype is 'n' and nu between 200 and 350 then Rs 7/- per unit
4. if mtype is 'n' and nu > 350 then Rs 9/- unit
Show mno, smr,emr,mtype, nu, tot-amt
Note: Above values should also update in the table
Output
SQL> @ filename.sql
Enter Mno : 900
Smr = 100
Emr = 275
Mtype = N
Nu = xx
Tot amt = xx
SQL> Select * from electricity
MNO SMR EMR MTYPE NU TOTAMT REMARKS
-----------------------------------------------------------------------
801 100 250 D
802 245 500 D
900 100 275 N 175 xxxxx
901 450 800 N
902 200 455 X INVALID TYPE
----------------------------------------------------------------------