Cursors in PL/SQL are used to handle query results one row at a time. They are essential for processing individual rows returned by database queries. This section will cover the basics of cursors, their types, and how to use them effectively in PL/SQL.

What is a Cursor?

A cursor is a pointer to the context area, which is a memory location where the Oracle database processes SQL statements. Cursors allow you to fetch and manipulate query results row by row.

Types of Cursors

There are two main types of cursors in PL/SQL:

  1. Implicit Cursors: Automatically created by Oracle for single SQL statements.
  2. Explicit Cursors: Defined by the programmer for queries that return multiple rows.

Implicit Cursors

Implicit cursors are automatically created by Oracle whenever an SQL statement is executed. They are simple to use and are suitable for single-row queries.

Example of Implicit Cursor

DECLARE
    v_employee_name employees.first_name%TYPE;
BEGIN
    SELECT first_name INTO v_employee_name
    FROM employees
    WHERE employee_id = 100;

    DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
END;
/

Explanation

  • DECLARE: Declares a variable v_employee_name of the same type as the first_name column in the employees table.
  • BEGIN: Starts the execution block.
  • SELECT INTO: Uses an implicit cursor to fetch the first_name of the employee with employee_id 100 into the variable v_employee_name.
  • DBMS_OUTPUT.PUT_LINE: Outputs the employee's name.
  • END: Ends the execution block.

Explicit Cursors

Explicit cursors are defined by the programmer and provide more control over the context area. They are used for queries that return multiple rows.

Steps to Use Explicit Cursors

  1. Declare the Cursor: Define the cursor with a SELECT statement.
  2. Open the Cursor: Allocate memory for the cursor.
  3. Fetch the Data: Retrieve data from the cursor into PL/SQL variables.
  4. Close the Cursor: Release the memory associated with the cursor.

Example of Explicit Cursor

DECLARE
    CURSOR emp_cursor IS
        SELECT first_name, last_name FROM employees;
    v_first_name employees.first_name%TYPE;
    v_last_name employees.last_name%TYPE;
BEGIN
    OPEN emp_cursor;
    LOOP
        FETCH emp_cursor INTO v_first_name, v_last_name;
        EXIT WHEN emp_cursor%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE('Employee: ' || v_first_name || ' ' || v_last_name);
    END LOOP;
    CLOSE emp_cursor;
END;
/

Explanation

  • DECLARE: Declares a cursor emp_cursor with a SELECT statement and variables v_first_name and v_last_name.
  • OPEN: Opens the cursor emp_cursor.
  • LOOP: Starts a loop to fetch data from the cursor.
  • FETCH INTO: Retrieves the first_name and last_name into the variables.
  • EXIT WHEN: Exits the loop when there are no more rows to fetch.
  • DBMS_OUTPUT.PUT_LINE: Outputs the employee's name.
  • CLOSE: Closes the cursor emp_cursor.

Practical Exercise

Exercise

Write a PL/SQL block that uses an explicit cursor to fetch and display the department_name and location_id from the departments table.

Solution

DECLARE
    CURSOR dept_cursor IS
        SELECT department_name, location_id FROM departments;
    v_department_name departments.department_name%TYPE;
    v_location_id departments.location_id%TYPE;
BEGIN
    OPEN dept_cursor;
    LOOP
        FETCH dept_cursor INTO v_department_name, v_location_id;
        EXIT WHEN dept_cursor%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE('Department: ' || v_department_name || ', Location ID: ' || v_location_id);
    END LOOP;
    CLOSE dept_cursor;
END;
/

Explanation

  • DECLARE: Declares a cursor dept_cursor with a SELECT statement and variables v_department_name and v_location_id.
  • OPEN: Opens the cursor dept_cursor.
  • LOOP: Starts a loop to fetch data from the cursor.
  • FETCH INTO: Retrieves the department_name and location_id into the variables.
  • EXIT WHEN: Exits the loop when there are no more rows to fetch.
  • DBMS_OUTPUT.PUT_LINE: Outputs the department name and location ID.
  • CLOSE: Closes the cursor dept_cursor.

Common Mistakes and Tips

  • Not Closing Cursors: Always close your cursors to free up resources.
  • Incorrect Data Types: Ensure that the variables used in FETCH INTO have the correct data types.
  • Handling No Data Found: Use EXIT WHEN cursor_name%NOTFOUND to handle cases where no data is found.

Conclusion

In this section, you learned about cursors in PL/SQL, including implicit and explicit cursors. You now know how to declare, open, fetch from, and close explicit cursors. Understanding cursors is crucial for handling query results efficiently in PL/SQL. In the next section, we will delve into exception handling to manage errors gracefully in your PL/SQL programs.

© Copyright 2024. All rights reserved