LOOPS in PL/SQL


In PL/SQL, loops are used to execute a block of statements repeatedly. There are several types of loops available in PL/SQL:

Basic Loop
WHILE Loop
FOR Loop

1. Basic Loop

The basic loop executes the statements inside the loop body repeatedly until an EXIT condition is met.

Syntax:
LOOP
    -- statements
    EXIT WHEN condition;
END LOOP;

Example:
DECLARE
    i NUMBER := 1;
BEGIN
    LOOP
        DBMS_OUTPUT.PUT_LINE('Iteration: ' || i);
        i := i + 1;
        EXIT WHEN i > 5;
    END LOOP;
END;

In this example, the loop runs five times, printing the iteration number each time.

2. WHILE Loop

The WHILE loop executes the statements as long as the condition is TRUE.

Syntax:
WHILE condition LOOP
    -- statements
END LOOP;

Example:
DECLARE
    i NUMBER := 1;
BEGIN
    WHILE i <= 5 LOOP
        DBMS_OUTPUT.PUT_LINE('Iteration: ' || i);
        i := i + 1;
    END LOOP;
END;

This WHILE loop works similarly to the basic loop example but uses a WHILE condition to control the loop.

3. FOR Loop

The FOR loop is used to execute a block of statements a fixed number of times. It automatically handles the loop variable initialization, increment, and termination.

Syntax:
FOR loop_variable IN [REVERSE] lower_bound..upper_bound LOOP
    -- statements
END LOOP;

Example:
BEGIN
    FOR i IN 1..5 LOOP
        DBMS_OUTPUT.PUT_LINE('Iteration: ' || i);
    END LOOP;
END;

In this example, the FOR loop iterates from 1 to 5 and prints the iteration number each time. The REVERSE keyword can be used to iterate in the reverse order.


Real time example with loops
CREATE TABLE employees (
    employee_id INTEGER PRIMARY KEY,
    first_name VARCHAR2(50),
    last_name VARCHAR2(50),
    salary NUMBER
);
DECLARE
    -- Loop variable
    emp_id INTEGER := 1;
    -- Employee record type
    TYPE emp_record_type IS RECORD (
        employee_id employees.employee_id%TYPE,
        first_name employees.first_name%TYPE,
        last_name employees.last_name%TYPE,
        salary employees.salary%TYPE
    );
    emp_record emp_record_type;
BEGIN
    -- Insert sample data
    INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (1, 'John', 'Doe', 50000);
    INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (2, 'Jane', 'Smith', 60000);
    INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (3, 'Jim', 'Brown', 55000);
    INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (4, 'Jill', 'White', 70000);
    
    COMMIT;
    
    -- Loop to update salaries with a 10% raise
    LOOP
        SELECT employee_id, first_name, last_name, salary INTO emp_record
        FROM employees
        WHERE employee_id = emp_id;
        
        emp_record.salary := emp_record.salary * 1.10;
        
        UPDATE employees
        SET salary = emp_record.salary
        WHERE employee_id = emp_id;
        
        emp_id := emp_id + 1;
        
        EXIT WHEN emp_id > 4;
    END LOOP;
    
    -- FOR loop to print updated employee details
    FOR emp IN (SELECT employee_id, first_name, last_name, salary FROM employees) LOOP
        DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp.employee_id || ', Name: ' || emp.first_name || ' ' || emp.last_name || ', Salary: ' || emp.salary);
    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