Conditional Statements in PL / SQL

In many case we would be in a state whether we have to choose Yes or No. These types of questions are also called Decision Statements. Here, the user is compelled to choose one answer. In this case we choose the answer based on the condition in which we are present. In the same way, Every Program has its moments like choosing if this condition is positive then we choose Yes statement else if condition is false then we choose No statement while coding.

The Every Programming Language has a few Conditional statements. These Conditional statements are also known as Decision Statements. There are a few Conditional Statements or Decision Statements in PL / SQL (Procedure Language / Structured Query Language).

The Conditional Statements can only be used in BEGIN State.

Types of Conditional Statements

  1. if condition then statement
  2. if condition then else statement
  3. nested if condition then statements
  4. if condition then elseif condition then else statements

1.) if Condition then Statement

This is one of the first important PL / SQL Conditional Statement. In this statement only if the condition is true, then the output is going to be shown. Else in any other cases there is no opportunity for outputs.

Syntax

Example Queries

OUTPUT:

PL/SQL procedure successfully completed.

OUTPUT:

The Value is 10

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 5
old   4: N1:=&N1;
new   4: N1:=5;

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 15
old   4: N1:=&N1;
new   4: N1:=15;
The Value is 15

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 9
old   4:     N1:=&N1;
new   4:     N1:=9;

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 10
old   4:     N1:=&N1;
new   4:     N1:=10;
The Value is 10

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 23
old   4:     N1:=&N1;
new   4:     N1:=23;
The Value is 23

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 5
old   6:     N1:=&N1;
new   6:     N1:=5;
Enter value for n2: 4
old   7:     N2:=&N2;
new   7:     N2:=4;
The Value is 20

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 9
old   6:     N1:=&N1;
new   6:     N1:=9;
Enter value for n2: 11
old   7:     N2:=&N2;
new   7:     N2:=11;

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 10
old   6:     N1:=&N1;
new   6:     N1:=10;
Enter value for n2: 10
old   7:     N2:=&N2;
new   7:     N2:=10;
The Value is 100

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 12
old   6:     N1:=&N1;
new   6:     N1:=12;
Enter value for n2: 2
old   7:     N2:=&N2;
new   7:     N2:=2;
The Value is 24

PL/SQL procedure successfully completed.

2.) if Condition then else Statement

This is also one of the first important PL / SQL Conditional Statement. In this statement only if the condition is true, then the output is going to be shown. Else in any other cases there is opportunity for outputs to seen. If the if condition fails then the else statements are executed.

This means if condition is true If statements are executed. If not possible else statements are executed.

This kind of conditional statements are required for finding few real life problems like finding odd or even number, Checking the number is divisible by two, three, etc., Finding Largest Number, Finding Smallest Number, etc. many programs like this.

Syntax

Example Queries

OUTPUT:

The Values of N1 is greater than N2

PL/SQL procedure successfully completed.

OUTPUT:

The Values of N2 is smaller than N1
PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 15
old   6:     N1:=&N1;
new   6:     N1:=15;
Enter value for n2: 16
old   7:     N2:=&N2;
new   7:     N2:=16;

OUTPUT:

The Values of N1 is smaller than N2

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 10
old   6:     N1:=&N1;
new   6:     N1:=10;
Enter value for n2: 6
old   7:     N2:=&N2;
new   7:     N2:=6;
The Values of N2 is smaller than N1

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 15
old   6:     N1:=&N1;
new   6:     N1:=15;
Enter value for n2: 26
old   7:     N2:=&N2;
new   7:     N2:=26;
The Values of N2 is greater than N1

PL/SQL procedure successfully completed.

OUTPUT:

Enter value for n1: 12
old   6:     N1:=&N1;
new   6:     N1:=12;
Enter value for n2: 11
old   7:     N2:=&N2;
new   7:     N2:=11;
The Values of N1 is greater than N2
PL/SQL procedure successfully completed.


Enter value for n1: 5
old   6:     N1:=&N1;
new   6:     N1:=5;
ODD NUMBER

PL/SQL procedure successfully completed.


Enter value for n1: 6
old   6:     N1:=&N1;
new   6:     N1:=6;
EVEN NUMBER

PL/SQL procedure successfully completed.

3.) if condition elsif condition else statements

This is also one of the first important PL / SQL Conditional Statement. In this statement only if the condition is true, then the output is going to be shown. Else in any other cases there is opportunity for outputs to seen. If the if condition fails then the else if statements with a condition are executed. Even if else if condition is False, then the else statements are executed.

Syntax

Example Queries

Output:

NUM2 SMALLER THAN NUM1

PL/SQL procedure successfully completed.

Output:

EQUAL

PL/SQL procedure successfully completed.

Output:

NUM1 SMALLER THAN NUM2

PL/SQL procedure successfully completed.

Input:

Output:

EQUAL

PL/SQL procedure successfully completed.

Input:

Output:

NUM1 SMALLER THAN NUM2

PL/SQL procedure successfully completed.

Input:

Output:

NUM2 SMALLER THAN NUM1

PL/SQL procedure successfully completed.

4.) Nested if condition then statements

This is also one of the conditional statements used in PL / SQL. Here, we are going to write the same if else statements. But in place of if statements we are going to write another if condition else statements. The workings of these statements are same and similar to the previous if else statements.

Syntax

Example Queries

Output:

The greatest number is 17

PL/SQL procedure successfully completed.

Output:

The greatest number is 12

PL/SQL procedure successfully completed.

Output:

The greatest number is 10
PL/SQL procedure successfully completed.

Input:

Output:

The greatest number is 20

PL/SQL procedure successfully completed.

Input:

Output:

The greatest number is 16
PL/SQL procedure successfully completed.

Input:

Output:

The greatest number is 21

PL/SQL procedure successfully completed.

Input:

Output:

The greatest number is 306

PL/SQL procedure successfully completed.

This is all about PL / SQL Conditional Expressions.






Latest Courses