Cursors
Cursors in PL/SQL are used to handle query results one row at a time. They allow you to iterate through result sets and perform operations on each row.
1. Introduction to Cursors
A cursor is a pointer to the context area, where SQL statements and their results are processed. Cursors allow row-by-row processing of query results.
Types of Cursors:
- Implicit Cursor:
- Automatically created by PL/SQL for SQL statements that return a single row or manipulate data (
INSERT
,UPDATE
,DELETE
).
- Automatically created by PL/SQL for SQL statements that return a single row or manipulate data (
- Explicit Cursor:
- Defined explicitly by the programmer to handle queries that return multiple rows.
2. Implicit Cursors
Implicit cursors are automatically created by PL/SQL for simple SQL operations.
Example: Using Implicit Cursor
Key Points:
- You don't explicitly declare or control the cursor.
- Used for SQL statements returning only one row.
3. Explicit Cursors
Explicit cursors are declared by the programmer for queries that return multiple rows.
Steps to Use Explicit Cursors:
- DECLARE: Define the cursor with the SQL query.
- OPEN: Open the cursor to establish the result set.
- FETCH: Retrieve rows one at a time.
- CLOSE: Release the resources associated with the cursor.
Syntax:
Example: Using Explicit Cursor
Explanation:
- Declares a cursor
emp_cursor
to fetch data from theemployees
table. - Loops through each row and processes the fetched data.
4. Cursor FOR Loops
Cursor FOR
loops simplify the use of explicit cursors by handling OPEN
, FETCH
, and CLOSE
operations automatically.
Syntax:
Example:
Explanation:
- The
FOR
loop automatically opens, fetches, and closes the cursor. emp_rec
is an implicit record variable that holds the fetched data.
5. Parameterized Cursors
Parameterized cursors allow you to pass parameters to the cursor query.
Syntax:
Example:
Explanation:
- The
dept_id
parameter is passed to filter employees by department.
6. Cursor Attributes
Cursor attributes provide information about the state of a cursor.
Attribute | Description |
---|---|
%FOUND | TRUE if the last FETCH returned a row. |
%NOTFOUND | TRUE if the last FETCH did not return a row. |
%ISOPEN | TRUE if the cursor is open. |
%ROWCOUNT | Returns the number of rows fetched so far. |
Example: Using Cursor Attributes
7. Example: Fetching Data from a Table Using Cursors
Scenario: Fetch employees from the employees
table whose salary is greater than 5000.
Summary
Implicit Cursors:
- Automatically created by PL/SQL for simple SQL queries.
Explicit Cursors:
- Used for queries that return multiple rows.
- Must be explicitly declared, opened, fetched, and closed.
Cursor FOR Loops:
- Simplify cursor handling by automating
OPEN
,FETCH
, andCLOSE
.
- Simplify cursor handling by automating
Parameterized Cursors:
- Allow passing parameters to filter data dynamically.
Cursor Attributes:
- Provide information about cursor state and operations:
%FOUND
,%NOTFOUND
,%ISOPEN
,%ROWCOUNT
.
- Provide information about cursor state and operations:
By mastering cursors, you can efficiently handle query result sets and perform row-by-row processing, enabling robust database applications.
Leave a Comment