This Oracle tutorial explains how to use the IF-THEN-ELSE statement in Oracle with syntax and examples.
In Oracle, the IF-THEN-ELSE statement is used to execute code when a condition is TRUE, or execute different code if the condition evaluates to FALSE.
There are different syntaxes for the IF-THEN-ELSE statement.
The syntax for IF-THEN in Oracle/PLSQL is:
You use the the IF-THEN syntax, when you want to execute statements only when condition is TRUE.
The syntax for IF-THEN-ELSE in Oracle/PLSQL is:
You use the IF-THEN-ELSE syntax, when you want to execute one set of statements when condition is TRUE or a different set of statements when condition is FALSE.
The syntax for IF-THEN-ELSIF in Oracle/PLSQL is:
You use the IF-THEN-ELSIF syntax, when you want to execute one set of statements when condition1 is TRUE or a different set of statements when condition2 is TRUE.
The syntax for IF-THEN-ELSIF-ELSE in Oracle/PLSQL is:
You use the IF-THEN-ELSIF-ELSE syntax, when you want to execute one set of statements when condition1 is TRUE, a different set of statements when condition2 is TRUE, or a different set of statements when all previous conditions (ie: condition1 and condition2) are FALSE.
The following is example using the IF-THEN-ELSE statement in an Oracle function:
In this IF-THEN-ELSE statement example, we've created a function called IncomeLevel. It has one parameter called name_in and it returns a varchar2. The function will return the income level based on the employee's name.