Skip to content

PL/SQL Cursors & Exception Handling (Practice) πŸš€ΒΆ

Prerequisites: PL/SQL Control Structures

Mentor's Note: In our PL/SQL Cursors Tutorial, we learned the theory. Now, let's solve the 7 specific problems from your RDBMS Lab assignment! πŸ’‘


🌟 The Scenario: The Patient Records πŸ₯ΒΆ

Imagine you are a doctor reviewing a list of patients.

  • Implicit Cursor: You look up One specific patient by their ID. πŸ”
  • Explicit Cursor: You have a Stack of Files (The Movie Reel 🎞️) and you go through them one by one. πŸ“¦
  • Exception: You search for a file, but it's Missing 🚫. You don't panic; you just handle the error safely. βœ…

πŸ’» Solved Assignment ProblemsΒΆ

πŸ”Ή Single Row Retrieval (Implicit Cursors)ΒΆ

1. Count EmployeesΒΆ

Problem: Count the total number of employees in the table.

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM EMPLOYEE;
    DBMS_OUTPUT.PUT_LINE('Total Employees: ' || v_count);
END;
/

2. Fetch Employee by IDΒΆ

Problem: Fetch name and salary using emp_id.

DECLARE
    v_name   EMPLOYEE.emp_name%TYPE;
    v_salary EMPLOYEE.salary%TYPE;
BEGIN
    SELECT emp_name, salary INTO v_name, v_salary
    FROM EMPLOYEE WHERE emp_id = &Enter_Employee_ID;

    DBMS_OUTPUT.PUT_LINE('Name: ' || v_name || ' πŸ‘€');
    DBMS_OUTPUT.PUT_LINE('Salary: β‚Ή' || v_salary);
END;
/

3. Handling NO_DATA_FOUNDΒΆ

Problem: Handle the error if a searched ID doesn't exist.

DECLARE
    v_name EMPLOYEE.emp_name%TYPE;
BEGIN
    SELECT emp_name INTO v_name FROM EMPLOYEE WHERE emp_id = &ID;
    DBMS_OUTPUT.PUT_LINE('Found: ' || v_name);
EXCEPTION
    WHEN NO_DATA_FOUND THEN
        DBMS_OUTPUT.PUT_LINE('⚠️ Error: No employee found with that ID.');
END;
/

πŸ”Ή Multiple Row Retrieval (Explicit Cursors)ΒΆ

4. Display All Names (Cursor Loop)ΒΆ

DECLARE
    CURSOR c_emp IS SELECT emp_name FROM EMPLOYEE;
    v_name EMPLOYEE.emp_name%TYPE;
BEGIN
    OPEN c_emp;
    LOOP
        FETCH c_emp INTO v_name;
        EXIT WHEN c_emp%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE('Employee: ' || v_name || ' βœ…');
    END LOOP;
    CLOSE c_emp;
END;
/

5. Filtered Cursor (Salary > 30,000)ΒΆ

DECLARE
    CURSOR c_high IS SELECT emp_name, salary FROM EMPLOYEE WHERE salary > 30000;
BEGIN
    FOR r IN c_high LOOP
        DBMS_OUTPUT.PUT_LINE(r.emp_name || ' earns β‚Ή' || r.salary);
    END LOOP;
END;
/

6. Join Cursor (Employee & Dept)ΒΆ

DECLARE
    CURSOR c_join IS 
        SELECT E.emp_name, D.dept_name 
        FROM EMPLOYEE E JOIN DEPARTMENT D ON E.dept_id = D.dept_id;
BEGIN
    FOR r IN c_join LOOP
        DBMS_OUTPUT.PUT_LINE(r.emp_name || ' works in ' || r.dept_name);
    END LOOP;
END;
/

7. Parameterized Cursor (By Dept ID)ΒΆ

DECLARE
    CURSOR c_dept (p_id NUMBER) IS 
        SELECT emp_name FROM EMPLOYEE WHERE dept_id = p_id;
BEGIN
    DBMS_OUTPUT.PUT_LINE('Employees in Dept &Dept_ID:');
    FOR r IN c_dept(&Dept_ID) LOOP
        DBMS_OUTPUT.PUT_LINE('- ' || r.emp_name);
    END LOOP;
END;
/

🎨 Visual Logic: The Exception Parachute¢

graph TD
    A[Start Block 🏁] --> B[Run Query πŸ”]
    B --> C{Record Found?}
    C -- Yes --> D[Show Data πŸ“€]
    C -- No --> E[Deploy Parachute: EXCEPTION πŸͺ‚]
    E --> F[Show Friendly Error πŸ’¬]
    D --> G[End βœ…]
    F --> G

πŸ’‘ Interview Tip πŸ‘”ΒΆ

"Interviewers love to ask: 'What happens if a SELECT INTO returns more than one row?' Answer: It throws a TOO_MANY_ROWS exception! Always use a cursor if you expect multiple results."


πŸ’‘ Pro Tip: "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill


πŸ“ˆ Learning PathΒΆ