Updated April 5, 2023
Definition of PL/SQL IF Statement
PL/SQL provides some decision-making statements to the user, in which IF one the decision-making statement that is provided by the PL/SQL. IF statement provides the functionality to either execute or skip the PL/SQL sequence of statements as per the requirement. Basically, “IF” statements provide different forms of “IF” statements to the user, such as IF-THEN, IF THEN ELSE, and IF-THEN ELSIF. The main advantage of the IF statement is that it provides the controls over the code for the execution that means the developer has the right to execute the code as per their requirement with a different form of IF statement.
Syntax:
Basically, there are three different syntaxes of IF statements; let’s see them one by one as follows.
1. IF-THEN
IF specified condition THEN
{Required statement for execution when the above condition is true…}
END IF;
Explanation
In the above syntax, we used the IF-THEN condition; when the IF condition is true, then it executed the THEN part of syntax that means whatever statement we required that we mentioned after the THEN keyword as shown in the above syntax. The last line of syntax indicates the end of the IF statement.
2. IF-THEN-ELSE
IF specified condition THEN
{Required statement for execution when the above condition is true…}
ELSE
{Required statement for execution when the above condition is false…}
END IF;
Explanation
In the above syntax, we use the IF THEN ELSE statement as shown, here we start with an IF statement that contains the specified condition that we require; if the specified condition is true, then it executes the THEN, and if the specified condition is false in IF statement, then it executes the ELSE part of the syntax.
3. IF-THEN-ELSIF
IF specified condition THEN
{Required statement for execution when the above condition is true…}
ELSEIF specified condition second THEN
{Required statement for execution when above-mentioned condition1 is false and specified condition second is true}
END IF;
Explanation
In the above syntax, we use the IF-THEN ELSIF statement; by using this syntax, we can execute the set of statements as per our requirement, and that statement depends on the conditions. The remaining execution of the above syntax is the same as the above two mentioned syntax.
How if statement works in PL/SQL?
Now let’s see how the IF statement works in PL/SQL as follows.
Basically, there are three different forms of IF statements as follows.
1. IF THEN:
The simplest version of the IF statement is this. Whether the collection of statements between THEN and END IF should be performed is determined by the condition between IF and THEN. The code is not run if the condition evaluates to FALSE or NULL.
2. IF THEN ELSE:
This combination provides either/or logic: run the code between THEN and ELSE or ELSE and END IF, depending on the condition between the IF and THEN keywords. The execution of one of these two portions of executable statements is carried out.
3. IF THEN ELSIF ELSE:
This most advanced variant of the IF statement chooses a TRUE condition from a list of mutually exclusive conditions and then executes the set of statements associated with that condition. If you’re using Oracle9i Database Release 1 or later, you should search CASE statements instead of IF statements.
Examples
Now let’s see the different examples of IF statements in PL/SQL for better understanding as follows.
Now let’s see all examples of IF statements one by one as follows.
SET SERVEROUTPUT ON;
DECLARE emp_salary number:=25000;
BEGIN
IF emp_salary>10000 THEN
dbms_output.put_line('Salary of emp is greater than 10 thousand');
END IF;
END;
/
Explanation
In the above example, we first need to set server output on then we can see the result of the written procedure. In this example, we first set the emp_salary is 25000as shown; after that, we use the IF statement and write the condition that(if emp_salary is greater than 10000) then print the salary of emp is greater than 10 thousand as shown in the above statement. The final output of the above statement we illustrated by using the following screenshot as follows.
Now let’s see the example of the IF THEN ELSE statement as follows.
SET SERVEROUTPUT ON;
DECLARE emp_salary number:=25000;
Increment number := 0;
BEGIN
IF emp_salary>10000 THEN
Increment :=emp_salary *10;
dbms_output.put_line('Emp salary is incremented by 10 percentage');
ELSE
Increment :=emp_salary * 0.5;
dbms_output.put_line('Emp salary is incremented by 0.5 percentage');
END IF;
END;
/
Explanation
In the above example, we try to implement the IF THEN ELSE statement, in this example, if emp_salary is greater than 10000, then it executes the THEN part that is emp_salary is incremented by10 percentage, and IF emp_salary is less than 10000, then it executes the ELSE part. In this example, THEN condition is executed. The final output of the above statement we illustrated by using the following screenshot as follows.
Now let’s see how ELSE statements will be executed as follows.
SET SERVEROUTPUT ON;
DECLARE emp_salary number:=5000;
Increment number := 0;
BEGIN
IF emp_salary>10000 THEN
Increment :=emp_salary *10;
dbms_output.put_line('Emp salary is incremented by 10 percentage');
ELSE
Increment :=emp_salary * 0.5;
dbms_output.put_line('Emp salary is incremented by 0.5 percentage');
END IF;
END;
/
Explanation
In this example, we try to implement the ELSE part of the above example; in this example, we just change the emp_salary 5000 instead of 25000; the remaining procedure is the same as the above example. The final output of the above statement we illustrated by using the following screenshot as follows.
Now let’s see the example of the IF THEN ELSEIF statement as follows.
SET SERVEROUTPUT ON;
DECLARE
emp_salary NUMBER := 500000;
Increment NUMBER( 20, 3 ) := 0;
BEGIN
IF emp_salary > 300000 THEN
Increment:= emp_salary * 0.2;
dbms_output.put_line('Emp salary is incremented by 0.2 percentage');
ELSIF emp_salary <= 300000 AND emp_salary > 200000 THEN
Increment:= emp_salary * 0.04;
dbms_output.put_line('Emp salary is incremented by 0.04 percentage');
ELSIF emp_salary <=2100000 AND emp_salary > 40000 THEN
Increment:= emp_salary * 0.09;
dbms_output.put_line('Emp salary is incremented by 0.09 percentage');
ELSE
Increment:= emp_salary * 0.02;
dbms_output.put_line('Emp salary is incremented by 0.02 percentage');
END IF;
END;
/
Explanation
In the above example, we try to implement the IF-THEN ELSEIF statement as shown. In this example, we wrote three different ELSEIF statements with the different conditions, as shown in the above example. The above-mentioned conditions depend on the user. The final output of the above statement we illustrated by using the following screenshot as follows.
As per emp_salary, the output will be different.
Conclusion
We hope from this article you learn PL/SQL IF statements. From the above article, we have learned the basic syntax of the IF statement, and we also see different examples of the IF statement. From this article, we learned how and when we use PL/SQL IF statements.
Recommended Articles
We hope that this EDUCBA information on “PL/SQL IF Statement” was beneficial to you. You can view EDUCBA’s recommended articles for more information.