top of page

PL SQL

Public·1 member

Bhanu Uday
Codersarts Employee

Codersarts Team

Java Developer

Conditional Statements in PL/SQL

Hi Everyone, Today's topic is conditional control(if,ifelse,case,goto,null).

PL/SQL supports the programming language features like conditional statements and iterative statements.

Its programming constructs are similar to how you use in programming languages like Java and C++.

Conditional Statements are basically used when condition occurs and we choose one statements.


IF

It allows you to either execute or skip a sequence of statements, depending on a condition.

The IF statement has the three forms:

1. IF THEN 2. IF THEN ELSE 3. IF THEN ELSIF


IF THEN :

IF condition   
THEN 
Statement:    -- It is executed when condition is true 
END IF;  

Example:

DECLARE num NUMBER := 234; 
BEGIN 
   IF num > 0 THEN 
      DBMS_OUTPUT.PUT_LINE( 'Number is Positive ' ); 
   END IF; 
END;

Output:

Statement processed.
Number is Positive 


IF THEN ELSE :

IF condition   
THEN 
Statement:    -- It is executed when condition is true 
ELSE
Statements      --statements to execute when condition is FALSE 
END IF;  

Example:


DECLARE num NUMBER := -234; 
BEGIN 
   IF num >= 0 
   THEN 
      DBMS_OUTPUT.PUT_LINE( 'Number is Positive ' ); 
   ELSE
    DBMS_OUTPUT.PUT_LINE( 'Number is Negative ' ); 

   END IF; 
END;

Output:

Statement processed.
Number is Negative 

IF THEN ELSEIF :

IF condition_1  
THEN 
Statement_1:    -- It is executed when condition_1 is true 
ELSEIF condition_2
THEN
Statement2      --statements to execute when condition_2 is TRUE 
ELSE
Statement3      --statements to execute when condition-2_is FALSE 
END IF;  

Example:



DECLARE
  e_salary NUMBER := 30000;
BEGIN
  IF e_salary > 20000 THEN
    e_salary := e_salary + 5000;
    dbms_output.put_line(' New Salary is : ' || e_salary  );  

  ELSIF e_salary <= 20000 AND e_salary > 10000 THEN 
    e_salary := e_salary + 2000;
    dbms_output.put_line(' New Salary is : ' || e_salary );  

  ELSIF e_salary <= 10000 AND e_salary > 5000 THEN 
    e_salary := e_salary + 1000 ;
   dbms_output.put_line(' New Salary is : ' || e_salary);  

  ELSE
    e_salary := e_salary + 5000;
    dbms_output.put_line(' New Salary is : ' || e_salary);  

  END IF;
END;


Output:

Statement processed.
 New Salary is : 35000

I will Upload CASE statement later. Keep Connected.

Thank you for reading.

Ask your Doubt or Query in Comment Sections.

9 Views
bottom of page