× PL/SQL - Overview PL/SQL - Basic Syntax PL/SQL - Data Types PL/SQL - Variables PL/SQL - Constants and Literals PL/SQL - Operators PL/SQL - Conditions PL/SQL -ifelse PL/SQL -elsif PL/SQL -nestedif PL/SQL -Case PL/SQL -Searched Case PL/SQL -Basic Loop PL/SQL -For Loop PL/SQL -While Loop PL/SQL - Strings PL/SQL - Arrays PL/SQL - Procedures PL/SQL - Functions PL/SQL - Cursors PL/SQL - Records PL/SQL - Exceptions PL/SQL - Triggers PL/SQL - Packages PL/SQL - Collections PL/SQL - Transactions PL/SQL - Date & Time PL/SQL - DBMS Output PL/SQL - Object Oriented
  • iconPLSQL Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

PL/SQL IF-THEN-ELSE statement


PL/SQL IF-THEN-ELSE statement
'if- then' is a keyword in PLSQL language .Which is used to execute one block of statements between 2 blocks
Syntax:
IF condition THEN 
   S1;  
ELSE  
   S2; 
END IF;
In the above syntax 'if' condition is true the statements of 'if' block is execute.'if' the condition is false the statements of 'else' block is execute
Example #1
DECLARE 
   a number(2) := 10; 
BEGIN 
   IF( a < 20 ) THEN 
      
      dbms_output.put_line('a is less than 20' ); 
   else
       dbms_output.put_line('a is not less than 20' ); 
   END IF; 
   
END; 
/


Example #2
DECLARE 
   c_id customers.id%type := 1; 
   c_sal  customers.salary%type; 
BEGIN 
   SELECT  salary  
   INTO  c_sal 
   FROM customers 
   WHERE id = c_id; 
   IF (c_sal <= 2000) THEN 
      UPDATE customers  
      SET salary =  salary + 1000 
         WHERE id = c_id; 
      dbms_output.put_line ('Salary 1000 updated'); 
   else
        UPDATE customers  
      SET salary =  salary + 2000 
         WHERE id = c_id; 
      dbms_output.put_line ('Salary 2000 updated'); 
   END IF; 
END; 
/

Key Points

  • PL/SQL IF-THEN-ELSE statement
  • Image