IF statements in PL/SQL

In PL/SQL (Procedural Language/Structured Query Language), the IF statement is used to execute a block of code conditionally, based on whether a specified condition is true or false. Here are the basic forms of the IF statement in PL/SQL:

Simple IF Statement

This form executes a block of code if a condition is true.

IF condition THEN
    -- code to be executed if condition is true
END IF;

Example:
DECLARE
    salary NUMBER := 4000;
    bonus  NUMBER := 0;
BEGIN
    IF salary > 3000 THEN
        bonus := 500;
    END IF;
    DBMS_OUTPUT.PUT_LINE('Bonus: ' || bonus);
END;

Output
Bonus: 500

IF-THEN-ELSE Statement

This form executes one block of code if the condition is true, and another block if the condition is false.
 
 IF condition THEN
    -- code to be executed if condition is true
ELSE
    -- code to be executed if condition is false
END IF;
 
Example:
DECLARE
    salary NUMBER := 2500;
    bonus  NUMBER := 0;
BEGIN
    IF salary > 3000 THEN
        bonus := 500;
    ELSE
        bonus := 200;
    END IF;
    DBMS_OUTPUT.PUT_LINE('Bonus: ' || bonus);
END;

In this example, since the salary is not greater than 3000, the bonus is set to 200. The output will be:
Bonus: 200

IF-THEN-ELSIF Statement

This form allows for multiple conditions to be checked sequentially. It executes the block of code corresponding to the first true condition.
 
IF condition1 THEN
    -- code to be executed if condition1 is true
ELSIF condition2 THEN
    -- code to be executed if condition2 is true
ELSIF condition3 THEN
    -- code to be executed if condition3 is true
ELSE
    -- code to be executed if all conditions are false
END IF;
 
Example:
 DECLARE
    salary NUMBER := 2800;
    bonus  NUMBER := 0;
BEGIN
    IF salary > 3000 THEN
        bonus := 500;
    ELSIF salary > 2000 THEN
        bonus := 300;
    ELSE
        bonus := 100;
    END IF;
    DBMS_OUTPUT.PUT_LINE('Bonus: ' || bonus);
END;

In this example, since the salary is greater than 2000 but not greater than 3000, the bonus is set to 300. The output will be:
Bonus: 300

Real time example
CREATE TABLE employees (
    employee_id   NUMBER PRIMARY KEY,
    first_name    VARCHAR2(50),
    last_name     VARCHAR2(50),
    salary        NUMBER,
    department_id NUMBER
);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (1, 'John', 'Doe', 5000, 10);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (2, 'Jane', 'Smith', 6000, 20);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (3, 'Emily', 'Jones', 7000, 10);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (4, 'Michael', 'Brown', 8000, 30);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (5, 'Sarah', 'Davis', 9000, 20);

DECLARE
    v_employee_id employees.employee_id%TYPE;
    v_first_name employees.first_name%TYPE;
    v_last_name employees.last_name%TYPE;
    v_salary employees.salary%TYPE;
    v_department_id employees.department_id%TYPE;
    v_raise_amount NUMBER;
BEGIN
    FOR emp_rec IN (SELECT employee_id, first_name, last_name, salary, department_id FROM employees)
    LOOP
        v_employee_id := emp_rec.employee_id;
        v_first_name := emp_rec.first_name;
        v_last_name := emp_rec.last_name;
        v_salary := emp_rec.salary;
        v_department_id := emp_rec.department_id;

        IF v_department_id = 10 THEN
            v_raise_amount := v_salary * 0.10;  -- 10% raise for department 10
        ELSIF v_department_id = 20 THEN
            v_raise_amount := v_salary * 0.05;  -- 5% raise for department 20
        ELSE
            v_raise_amount := v_salary * 0.03;  -- 3% raise for other departments
        END IF;

        UPDATE employees
        SET salary = salary + v_raise_amount
        WHERE employee_id = v_employee_id;

        DBMS_OUTPUT.PUT_LINE('Employee ' || v_first_name || ' ' || v_last_name || ' in department ' || v_department_id || ' received a raise of ' || v_raise_amount);
    END LOOP;
END;
/


Comments

Popular posts from this blog

KTU DBMS LAB CSL 333 BTech S5 - Dr Binu V P

KTU DBMS LAB CSL 333 Syllabus and Experiments

Creating a Database and Table ,executing queries - MySQL Lab Exercise