0% found this document useful (0 votes)
53 views

Chapter 04

Oracle Engine defines a work area for its internal processing, which is called a cursor. Cursors are of two types: Implicit Cursors: Oracle Engine automatically opens these cursors when the SQL statements are executed. Explicit Cursors: Database administrator declares cursors within the PL / SQL code block.

Uploaded by

Juan Tello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Chapter 04

Oracle Engine defines a work area for its internal processing, which is called a cursor. Cursors are of two types: Implicit Cursors: Oracle Engine automatically opens these cursors when the SQL statements are executed. Explicit Cursors: Database administrator declares cursors within the PL / SQL code block.

Uploaded by

Juan Tello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Understanding PL/SQL using Oracle Database

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

Copyright 2006, Tata Consultancy Services Limited (TCS).

Understanding PL/SQL using Oracle Database

Introducing Cursors

1.1 4.1 Types of Cursors


Oracle Engine defines a work area for its internal processing, which is called a cursor.
Oracle Engine uses a cursor to execute SQL statements and store the data retrieved from
execution of SQL queries. In addition, the data stored in the cursor is called the Active
Data Set. You can also use cursors to access and execute rows returned by the SELECT
statement, one at a time. You can also give a name to a cursor for future reference.
Oracle Engine opens a cursor when a SQL statement is executed. The result set of the
executed SQL query is stored in the cursor that is opened by the Oracle Engine in the
memory. After the execution of the SQL statements is complete, this data is transferred to
the required user.
Cursors are of two types:
Implicit Cursors: Oracle Engine automatically opens these cursors when the SQL
statements are executed. Oracle opens this cursor for its internal processing and
therefore user cannot process the information in the implicit cursor.
Explicit Cursors: Database administrator declares these cursors within the
PL/SQL code block. This cursor is defined by the program for any query that
returns two or more rows of data.
4.1.1

Implicit Cursors

The working process of an implicit cursor is:


PL/SQL block issues the implicit cursor every time the SQL statement is
executed, if the explicit cursor does not exist for the specified SQL statement.
This cursor is automatically connected to the DML statements, such as INSERT,
DELETE, and UPDATE.
The UPDATE and DELETE statements have cursors. These cursors identify the
set of rows that will be affected by the specified DML operation.
The implicit cursor creates a place for an INSERT statement, where it receives the
data that is to be inserted in the database.
The most recently opened cursor is known as SQL% cursor.
The implicit cursor has four attributes, as shown in the table.
Attribute Name
%ISOPEN
%FOUND
%NOTFOUND

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).

Understanding PL/SQL using Oracle Database

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

Copyright 2006, Tata Consultancy Services Limited (TCS).

Understanding PL/SQL using Oracle Database

Introducing Cursors

1.2 4.2 Working with Cursors


Explicit cursors are more efficient as compared to the implicit cursors as the database
administrator can control these cursors. The working process of the explicit cursor is:
Declaring the cursor: In this part, the cursor is initialised.
Opening the cursor: After the cursor is initialised, the memory is allocated to the
cursor.
Fetching the cursor: The data is retrieved from the cursor and stored in the
memory variables.
Closing the cursor: This part involves the release of the allocated memory.
4.2.1

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:

Oracle checks all the variables in the WHERE clause.


The active data set is determined based on the value of the variables.
PL/SQL identifies the active set of data.
The active set pointer is set to the first row.

The syntax for opening a cursor is shown in listing 4.3.


OPEN <cursor name>
Listing 4.3
Syntax of Opening a Cursor

Copyright 2006, Tata Consultancy Services Limited (TCS).

Understanding PL/SQL using Oracle Database

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.

FETCH <cursor name> INTO PL/SQL variable | record;


Listing 4.4
Syntax of Fetching a Cursor

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

Explicit Cursor Attributes

The explicit cursor has four attributes, as shown in the table.


Attribute Name
%ISOPEN
%FOUND
%NOTFOUND
%ROWCOUNT

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.

Copyright 2006, Tata Consultancy Services Limited (TCS).

Understanding PL/SQL using Oracle Database

Introducing Cursors

SET SERVEROUTPUT ON;


DECLARE
CURSOR c_name IS
SELECT ename from emp
where sal > 2000;
vr_name c_name%ROWTYPE;
BEGIN

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

1.3 4.3 Cursor FOR Loop and Nesting Cursors


Cursor FOR loop is a method of handling cursors. This method simplifies the code
structure of the PL/SQL block. In this method, the process of opening, fetching, and
closing the cursor is performed implicitly. Using the cursor FOR loop, the set of
statements are repeated once for each row returned by the cursor.
For example, you as a database administrator want to create a table, tbl with one column,
NAME. In this table, you want to insert the name of the employees whose salary is less
than 1500. To do this, you need to specify the code shown in listing 4.7.

Copyright 2006, Tata Consultancy Services Limited (TCS).

Understanding PL/SQL using Oracle Database

Introducing Cursors

CREATE TABLE tbl(NAME VARCHAR2(100));


DECLARE
CURSOR c_emp IS
SELECT ename
FROM emp
WHERE sal < 1500;
BEGIN

FOR v_emp IN c_emp

LOOP

INSERT INTO tbl


VALUES(v_emp.ename);
END LOOP;
END;
Listing 4.7
Illustration of Cursor FOR 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.

Copyright 2006, Tata Consultancy Services Limited (TCS).

Understanding PL/SQL using Oracle Database

Introducing Cursors

SET SERVEROUTPUT ON;


DECLARE
v_emp VARCHAR2(100);
v_deptno emp.deptno%TYPE;
CURSOR c_emp IS
SELECT ename
FROM emp
WHERE sal <= 800;
CURSOR c_deptno IS
SELECT deptno
FROM emp
WHERE job = 'CLERK';
BEGIN
FOR v_emp IN c_emp LOOP
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME" '||v_emp.ename);
FOR v_deptno IN c_deptno LOOP
DBMS_OUTPUT.PUT_LINE('Dept No :'||v_deptno.deptno);
END LOOP;
END LOOP;
END;
Listing 4.8
Illustration of Nested Cursors

Figure 4.4
Result of Listing 4.8

Copyright 2006, Tata Consultancy Services Limited (TCS).

Understanding PL/SQL using Oracle Database

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.

Copyright 2006, Tata Consultancy Services Limited (TCS).

Understanding PL/SQL using Oracle Database

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

Copyright 2006, Tata Consultancy Services Limited (TCS).

10

You might also like