PL/SQL Exception

PL/SQL Exception

PL/SQL Interview Questions

PL/SQL Interview Questions and Answers


PL/SQL If Then Statement

The IF statement in PL/SQL is used to check the specific condition before execute single or block of statements only when condition is TRUE. PL/SQL allows you to write programming language features like IF statement to make your code decision making. If the condition is "true" then block will be executed, otherwise block will not be executed.

PL/SQL IF THEN Statement Syntax

To check a specific condition before execute block statements only when condition is TRUE, use the following syntax:

Syntax

IF (condition)
THEN   
Statement: {It is executed when condition is true}  
END IF;   

PL/SQL IF THEN Statement example

The following PL/SQL IF THEN statement is used execute block statement only if condition is TRUE:

Example

DECLARE  
   salary number(7) := 10000;  
BEGIN    
   IF(salary > 15000 && <= 20000) THEN 
      DBMS_OUTPUT.PUT_LINE('You fall under 0% income tax bracket' );  
END;  

PL/SQL IF THEN ELSE Statement Syntax

To check a specific condition before execute block statements only when condition is TRUE, otherwise execute other block statements if condition is FALSE, use the following syntax:

Syntax

IF (condition)  
THEN  
   {statements to be executed when condition is TRUE}  
ELSE  
   {statements to be executed when condition is FALSE}  
END IF;     

PL/SQL IF THEN ELSE Statement example

The following PL/SQL IF THEN ELSE statement is used to execute block statement only if condition is TRUE otherwise execute other block statements if condition is FALSE:

Example

DECLARE  
   salary number(7) := 15000;  
BEGIN    
   IF( salary > 15000 && <= 20000) THEN   
      DBMS_OUTPUT.PUT_LINE('You fall under 10% income tax bracket' );  
   ELSE  
      DBMS_OUTPUT.PUT_LINE('You fall under 0% income tax bracket' );  
   END IF;   
END;  

PL/SQL IF THEN ELSIF Statement Syntax

To check a multiple conditions, if condition1 is TRUE execute block statement for contition one, otherwise execute other block statements for contition two, if condition2 is TRUE, use the following syntax:

Syntax

IF (condition1)  
THEN  
   {statements to be executed when condition1 is TRUE}  
ELSIF (condition2)
THEN  
   {statements to be executed when condition2 is TRUE}  
END IF;      

PL/SQL IF THEN ELSIF Statement example

The following PL/SQL IF THEN ELSIF statement is used to execute block statement only if condition1 is TRUE, otherwise execute other block statements for contition two, if condition2 is TRUE:

Example

DECLARE  
   salary number(7) := 55000;  
BEGIN    
   IF( salary > 15000 && <= 20000) THEN   
      DBMS_OUTPUT.PUT_LINE('You fall under 10% income tax bracket' );  
   ELSIF ( salary > 20001 && <= 80000) THEN
      DBMS_OUTPUT.PUT_LINE('You fall under 20% income tax bracket' );  
   END IF;   
END;   

PL/SQL IF THEN ELSIF ELSE Statement Syntax

To check a multiple conditions, if condition1 is TRUE execute block statement for contition one, execute other block statements for contition two, if condition2 is TRUE otherwise execute other block statement if condition1 and condition2 are FALSE, use the following syntax:

Syntax

IF (condition1)    
THEN  
   {statements to be executed when condition1 is TRUE}  
ELSIF (condition2)   
THEN  
   {statements to be executed when condition2 is TRUE}  
ELSE  
   {statements to be executed when condition1 and condition2 is FALSE}  
END IF;       

PL/SQL IF THEN ELSIF Statement example

The following PL/SQL IF THEN ELSIF statement is used to execute block statement only if condition1 is TRUE, execute other block statements for contition two, if condition2 is TRUE otherwise execute other block statement if condition1 and condition2 are FALSE:

Example

DECLARE  
   salary number(7) := 100000;  
BEGIN    
   IF( salary > 15000 && <= 20000) THEN   
      DBMS_OUTPUT.PUT_LINE('You fall under 10% income tax bracket' );  
   ELSIF ( salary > 20001 && <= 80000) THEN
      DBMS_OUTPUT.PUT_LINE('You fall under 20% income tax bracket' ); 
   ELSE
      DBMS_OUTPUT.PUT_LINE('You fall under 30% income tax bracket' ); 
   END IF;   
END;