Chapter 04
Chapter 04
Introducing Cursors
Introducing Cursors
SCOPE
4.1 Types of Cursors
4.1.1 Implicit Cursors
4.2 Working with Cursors
4.2.1 Declaring a Cursor
4.2.2 Opening a Cursor
4.2.3 Fetching a Cursor
4.2.4 Closing a Cursor
4.2.5 Explicit Cursor Attributes
4.3 Cursor FOR Loop and Nesting Cursors
Introducing Cursors
Implicit Cursors
Description
After executing the SELECT, DELETE, and INSERT statements,
Oracle automatically opens and closes the SQL cursor. Therefore,
you cannot refer this attribute outside its SQL statement.
Returns TRUE if DELETE, INSERT, or UPDATE statement affects
one or more rows, else it returns FALSE.
Returns FALSE if DELETE, INSERT, or UPDATE statement
Copyright 2006, Tata Consultancy Services Limited (TCS).
Attribute Name
%ROWCOUNT
Introducing Cursors
Description
affects one or more rows, else it returns TRUE.
Returns the number of rows affected by DELETE, INSERT, or
UPDATE statement.
Suppose, you as a database administrator want to find the name of the employee whose
empno is 7876. You also want to stores the resultant value in a variable, v_name. To do
this, you need to create a file, icur.sql and enter the code shown in listing 4.1.
SET SERVEROUTPUT ON;
DECLARE
v_name VARCHAR2(30);
BEGIN
Select ename into v_name
from emp
where empno = 7876;
DBMS_OUTPUT.PUT_LINE('employee name: '|| v_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('There is no employee with emp ED 7876');
END;
Listing 4.1
Illustration for Implicit Cursors
Figure 4.1
Result of Listing 4.1
Introducing Cursors
Declaring a Cursor
Oracle allows you to define the name of the cursor and associate it with a SELECT
statement. The syntax of declaring a cursor is shown in listing 4.2.
CURSOR <cursor name> IS select statement
Listing 4.2
Syntax of Declaring a Cursor
4.2.2
Opening a Cursor
When you open a cursor, there are four actions that take place automatically, which
include:
4.2.3
Introducing Cursors
Fetching a Cursor
After opening a cursor, PL/SQL allows you to retrieve the data from the cursor. This
process is known as fetching the cursor. The fetch command accesses one row at a time
from the active data set. The values of each row in the active data set are stored in the
respective variables. After each fetch, the active set pointer points to the next row.
Therefore, each row will return consecutive rows of the active set until the entire set is
returned. The syntax for fetching a cursor is shown in listing 4.4.
4.2.4
Closing a Cursor
After all the rows are accessed, you need to close the cursor. This process releases the
memory and other resources associated with it. The syntax for closing the cursor is shown
in listing 4.5.
CLOSE <cursor name>;
Listing 4.5
Syntax of Closing a Cursor
4.2.5
Description
Returns TRUE if cursor is open, else returns FALSE.
Returns TRUE if previous FETCH returned a row, else returns
FALSE.
Returns TRUE if cursor is open, else returns FALSE.
Number of rows fetched from a cursor at a time.
For example, you as a database administrator want to find the name of the employees
whose salary is greater than 2000. For this, you want to declare a cursor, c_name and
fetch the resultant value in a variable, vr_name. To do this, you need to specify the code
shown in listing 4.6.
Introducing Cursors
OPEN c_name;
LOOP
FETCH c_name INTO vr_name;
EXIT WHEN c_name%notfound;
DBMS_OUTPUT.PUT_LINE('Employee name: '||vr_name.ename);
END LOOP;
CLOSE c_name;
END;
Listing 4.6
Illustration of Explicit Cursor
Figure 4.2
Result of Listing 4.6
Introducing Cursors
LOOP
Figure 4.3
Result of Listing 4.7
Cursors can also be nested. For example, you as a database administrator want to find the
name of the employees whose salary is less than 800. You also want to find the
department number of all the employees whose designation is CLERK. To do this, you
need to create the SQL file, nestcur.sql and specify the code shown in listing 4.8.
Introducing Cursors
Figure 4.4
Result of Listing 4.8
Introducing Cursors
SUMMARY
Cursor is a work area that allows you to execute multiple rows in a database.
Cursor allows you to access and execute rows returned by the SELECT statement,
one at a time.
Implicit Cursors are automatically declared every time the SQL statement is
executed.
Explicit Cursors are declared by the database administrator within the PL/SQL
code block.
Explicit cursors are more efficient as compared to the implicit cursors as the
database administrator can control these cursors.
Cursor FOR loop is another method of handling cursors.
Introducing Cursors
SELF ASSESSMENT
I. Fill up the blanks:
1. ___cursor________ is a work area that allows you to execute multiple rows in a
database.
2. ___implicit_____ cursors are automatically declared every time the SQL statement is
executed.
3. _explicit_________ cursors are declared by the database administrator within the
PL/SQL code block.
4. __cursor for loop________ method simplifies the code structure of PL/SQL block..
5. After all the rows are accessed, you need to ____close_______ the cursor.
II. State True or False:
1. Trigger allows you to access and execute rows returned by the SELECT statement,
one at a time. F
2. Implicit cursor is defined by the program for any query that returns two or more rows
3. Explicit cursors are more efficient as compared to the implicit cursors as the database
administrator can control these cursors. T
4. Closing the cursor releases the memory and other resources associated with it. T
10