Introduction to IF THEN ELSE in Oracle
In Oracle IF THEN ELSE is a branching statement. This statement enables us to specify two different groups of statements for execution. This statement accepts Boolean expression as a condition, depending on the value of that expression IF THEN ELSE STATEMENT executes or skips a sequence of statements.
- IF: It is a reserved word and marks the beginning of the IF statement
- Condition: This is a Boolean expression that returns a Boolean result.
- Statement 1: This is a statement which executes if the condition evaluates the TRUE result
- ELSE: It executes if the condition returns the FALSE result. It is an option which enables statement.
- Statement 2:This is a statement that executes if the condition returns a result.
- ENDIF: This keyword closes or terminates the IF THEN ELSE block or statement
Flowchart of IF THEN ELSE in Oracle
Flowchart of IF … THEN … ELSE Block
Explanation: Both flowcharts clearly show that how IF THEN ELSE statement works. IF block accepts the condition and evaluates to TRUE / FALSE. If the condition returns TRUE then go to the “THEN” Block and executes the “STATEMENT 1” and then execute END block. If the condition returns FALSE then go to the “ELSE” Block and execute “STATEMENT 2” and then execute END block. If the condition evaluates to ‘NULL’ then ‘NULL’ will be treated as ‘FALSE’ then go to the “ELSE” Block and execute “STATEMENT 2” and then execute END block.
General Points to Ponder About It
Before implementing of Oracle IF THEN ELSE statement, must know some important points about IF THEN ELSE construct:
- “IF … THEN … ELSE” is a reserved word and marks the beginning of the “IF” statement.
- This enables us to specify two different groups of statements for execution.
- One group is evaluated when the condition evaluates to “TRUE”, the next group is evaluated when the condition evaluates to “FALSE”.
- The “END IF” is a reserved word that indicates the end of the “IF … THEN … ELSE” construct.
- When “IF … THEN” is executed, a condition is evaluated to either “TRUE” or “FALSE”.
- The condition need not be specified in brackets.
- Condition(s) is / are compared by using either the comparison operator(s) or SQL * PLUS operator(s).
- One “IF” keyword can manage any number of conditions at a point of time using the Logical Connectivity like “AND”, “OR”.
- Even though the multiple conditions need not be constrained by using brackets, it is better to control their precedence using the proper implementation of brackets to avoid ambiguity.
- Every “IF” that is implemented needs compulsorily a “TRUE” state evaluation for its successful execution. But an evaluation state is not mandatory when the condition is “FALSE”.
- After the actual job of “IF” has been completed, the control structure returns the job to the original state of PL / SQL block.
Example to Implement IF THEN ELSE in Oracle
In this section, we’ll see the implementation of Oracle IF … THEN … else how this control structure works in Oracle using some examples. For that, we will use the below sample table (Employee & Dept_category) if required with 14 & 8 records to understand the Oracle IF … THEN … ELSE behavior.
V_num NUMBER: = &EnterNumber;
IF MOD (V_num, 2) =0 THEN
DBMS_OUTPUT.PUT_LINE (V_num ||' is an even number.');
DBMS_OUTPUT.PUT_LINE (V_num ||' is an odd number.');
&EnterNumber: Here “&” enables a user to enter a value within the range and data type criteria.
- MOD (m, n): It is a predefined function that returns the remainder of being’ divided by ‘n’.
- DBMS_OUTPUT: DBMS_OUTPUT is an Oracle inbuilt package that allows us to write data to direct output to a screen. This package is used for displaying debugging information.
- PUT_LINE: It is a procedure that displays the information or results in a line
If MOD function returns 0 then equality condition evaluates “TRUE” and executes the THEN block statement else executes ELSE block.
As the output showing “ PL/SQL procedure completed” means no error in the above code. But the output is not throwing an expected result. WHY?
It not showing an expected result because there is no setting up control to display output. To set the control, need to execute once “ SET SERVER OUTPUT ON” in the current user session and it allows the buffer to display the output. See the output for the same example after running the “ SET SERVER OUTPUT ON” in the session.
In this output, we got the expected output “10 is an even number”.
The NULL effect in the above PL/SQL Program
In the above output entered value is NULL and in that scenario IF … THEN … ELSE executes the FALSE (ELSE) block that’s why output shows NULL in the place of V_num.
Tips: Whenever a conditional process is executed it is better to cross verify the NULL status of any variable or value before execution. To run any PL/ SQL program use forward slash “/” key. NULL must be handled carefully, NULL can be handled using nested IF or using some other condition.
Oracle IF … THEN … ELSE is a control structure that can be very useful when trying to choose between two mutually exclusive actions. It is used in the PL/SQL block which enables a programmer to handle the real-time requirement in a proper way. This structure operates on Boolean expression and Boolean result.
This is a guide to IF THEN ELSE in Oracle. Here we discuss an introduction to IF THEN ELSE in Oracle with syntax, flowchart, and example. You can also go through our other related articles to learn more –