Ticker

6/recent/ticker-posts

IF-THEN-ELSE Statement in Oracle PLSQL

Hello Readers, This is another blog post of PLSQL Tutorial. In this Oracle PLSQL Tutorial, we will explain the IF-THEN-ELSE Statement in Oracle PLSQL with syntax and Examples.

IF-THEN-ELSE Statement in Oracle PLSQL By Javainhand

IF-THEN-ELSE STATEMENT IN ORACLE SQL

In this post, we will discuss the below following topics:

  1. What is the IF-THEN-ELSE Statement in Oracle PLSQL
  2. Syntax of IF-THEN-ELSE Statement in Oracle PLSQL
  3. Example of IF-THEN-ELSE Statement in Oracle PLSQL

What is the IF-THEN-ELSE Statement in Oracle PLSQL

In the Oracle, the if-then-else statement is control structure statement that provides two alternative path of execution.

If the Boolean Expression(condition) evaluates to TRUE, it executes the sequence of statement inside the THEN block. if condition evaluates to FALSE or NULL, it executes the sequence of statement inside the ELSE block

It is an extension of  the simple IF-THEN Statement. By adding ELSE keyword, we guarantee that exactly one set of statement will execute based on the condition.

Syntax of  IF-THEN-ELSE Statement in Oracle PLSQL

IF condition THEN 
-- sequence_of_statements if condition is TRUE 
ELSE -- sequence_of_statements if condition is FALSE or NULL 
END IF;

Example of IF-THEN-ELSE Statement in Oracle PLSQL

Let's understand the IF-THEN-ELSE Statement in Oracle PLSQL with a simple example where we check if given number is even or odd.

DECLARE 
   V_NUMBER NUMBER := 15; 
BEGIN 
   -- Checking if the number is divided by 2 
   IF MOD(V_NUMBER, 2) = 0 THEN 
      DBMS_OUTPUT.PUT_LINE('The Given Number ' || V_NUMBER || ' is an EVEN Number.'); 
   ELSE 
      DBMS_OUTPUT.PUT_LINE('The Given Number ' || V_NUMBER || ' is an ODD Number.'); 
   END IF; 
END;
/

OUTPUT

The Given Number 15 is an ODD Number.

Example 2: Comparing Two Number

DECLARE 
   V_NUM1 NUMBER := 50; 
   V_NUM2 NUMBER := 100; 
BEGIN 
   IF V_NUM1 > V_NUM2 THEN 
      DBMS_OUTPUT.PUT_LINE(V_NUM1 || ' is greater than ' || V_NUM2); 
   ELSE 
      DBMS_OUTPUT.PUT_LINE(V_NUM2 || ' is greater than ' || V_NUM1); 
   END IF; 
END;
/

That's all for today, I think (IF-THEN-ELSE Statement in Oracle PLSQL) post will be helpful for you. If you like the post then share your view in the comment box.

If you want to watch this topic then click here

Post a Comment

0 Comments