IF Statement in PL/SQL
The IF statement in PL/SQL is a conditional control structure that allows the database to execute specific blocks of code only when certain conditions are met.
There are three forms of the IF statement:
1. IF-THEN (Simple)
Executes a block of code only if the condition is TRUE. If the condition is FALSE or NULL, the code is skipped.
IF salary > 50000 THEN
bonus := 5000;
END IF;
2. IF-THEN-ELSE (Two-way)
Provides an alternative block of code to run if the condition is FALSE or NULL.
IF age >= 18 THEN
status := ‘Eligible’;
ELSE
status := ‘Not Eligible’;
END IF;
3. IF-THEN-ELSIF (Multi-way)
Allows you to check multiple conditions in sequence. It stops at the first TRUE condition it finds.
IF grade >= 90 THEN result := ‘A’;
ELSIF grade >= 80 THEN result := ‘B’;
ELSE result := ‘C’;
END IF;