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_ROWSexception! 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