if else plsql

PL/SQL If statement:

If statement is used to execute a block of statements if specified condition is true. Commonly used PL/SQL If statement:

IF-THEN statement:

Syntax:

IF condition   
THEN   
   //Block of statements1
END IF;

Block of statements1 executes when the specified condition is true.

IF-THEN-ELSE statement:

Syntax:

IF condition   
THEN  
      //Block of statements1
ELSE  
      //Block of statements2
END IF;

Block of statements1 executes when the specified condition is true otherwise Block of statements2 executes.

IF-THEN-ELSIF statement:

Syntax:

IF condition1
THEN  
      //Block of statements1
ELSIF condition2
      //Block of statements2
ELSE  
      //Block of statements3
END IF;

Block of statements1 executes when condition1 is true if false codition2 is checked and Block of statements2 executes if condition2 is true and so on. Block of statements in ELSE block executes when no condition is true.

Example:

DECLARE
   var number(3) := 50;
BEGIN
   IF (var = 10) THEN
      dbms_output.put_line('Value of var is 10');
   ELSIF (var = 20) THEN
      dbms_output.put_line('Value of var is 20');
   ELSIF (var = 30) THEN
      dbms_output.put_line('Value of var is 30');
   ELSE
       dbms_output.put_line('None of the above condition is true.');
   END IF;
   dbms_output.put_line('Exact value of var is: '|| var); 
END;
/

Output:

None of the above condition is true.
Exact value of var is: 50

 

Please follow and like us:
Content Protection by DMCA.com