Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

CURSORS

CSC Proprietary and Confidential

CURSOR MANIPULATION

To process an SQL statement, ORACLE needs to


create an area of memory known as the context
area; this will have the information needed to
process the statement.

A cursor is a pointer to this context area. PL/SQL


controls the context area through a cursor. A
cursor holds the rows (one or more) returned by a
SQL statement. The set of rows the cursor holds is
referred to as the active set.

CSC Proprietary and Confidential

Types Of Cursors

There are two types of cursors:

1. An IMPLICIT cursor is automatically declared by


Oracle every time an SQL statement is executed.
The user will not be aware of this happening and will
not be able to control or process the information in an
implicit cursor.
2. An EXPLICIT cursor is defined by the program for
any query that returns more than one row of data.
That means the programmer has declared the cursor
within the PL/SQL code block.

CSC Proprietary and Confidential

IMPLICIT CURSOR
1. A cursor is automatically associated with every DML
(Data Manipulation) statement (UPDATE, DELETE,
INSERT).
2. All UPDATE and DELETE statements have cursors that
identify the set of rows that will be affected by the
operation.
3. An INSERT statement needs a place to receive the data
that is to be inserted in the database; the implicit cursor
fulfills this need.

CSC Proprietary and Confidential

The Processing Of An Implicit Cursor

The implicit cursor is used to process INSERT, UPDATE,


DELETE, and SELECT INTO statements.

During the processing of an implicit cursor, Oracle


automatically performs the OPEN, FETCH, and CLOSE
operations.

An implicit cursor cannot tell you how many rows were


affected by an update. SQL%ROWCOUNT returns
numbers of rows updated. It can be used as follows:

CSC Proprietary and Confidential

BEGIN

UPDATE student

SET first_name = 'Bean'

WHERE first_name LIKE 'B%';

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT);

END;

EXPLICIT CURSOR

The only means of generating an explicit cursor is


for the cursor to be named in the DECLARE
section of the PL/SQL Block.

The advantages of declaring an explicit cursor


over the indirect implicit cursor are that the explicit
cursor gives more programmatic control to the
programmer.

Implicit cursors are less efficient than explicit


cursors and thus it is harder to trap data errors.

CSC Proprietary and Confidential

EXPLICIT CURSOR
The process of working with an explicit cursor consists
of the following steps:
DECLARING the cursor. This initializes the cursor
into memory.
OPENING the cursor. The previously declared
cursor can now be opened; memory is allotted.
FETCHING the cursor. The previously declared
and opened cursor can now retrieve data; this is
the process of fetching the cursor.
CLOSING the cursor. The previously declared,
opened, and fetched cursor must now be closed to
release memory allocation.
7
CSC Proprietary and Confidential

Example

DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is SELECT id, name, address
FROM customers;
BEGIN

OPEN c_customers;
LOOP

FETCH c_customers into c_id, c_name, c_addr;


EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;

CLOSE c_customers;
END;

CSC Proprietary and Confidential

CSC Proprietary and Confidential

USING CURSOR FOR LOOPS AND


NESTING CURSORS

When using the cursor FOR LOOP, the process of


opening, fetching, and closing are implicitly handled.

This makes the blocks much simpler to code and


easier to maintain.

The cursor FOR LOOP specifies a sequence of


statements to be repeated once for each row
returned by the cursor.

Use the cursor FOR LOOP if you need to FETCH


and PROCESS each and every record from a
cursor.

CSC Proprietary and Confidential

10

Example
DECLARE
CURSOR c_student IS
SELECT student_id, last_name, first_name
FROM student
WHERE student_id < 110;
BEGIN
FOR r_student IN c_student
LOOP
INSERT INTO table_log
VALUES(r_student.last_name);
END LOOP;
END;
CSC Proprietary and Confidential

11

CURSORS WITH PARAMETERS

A cursor can be declared with parameters.

This enables a cursor to generate a specific result set, which is, on the
one hand, more narrow, but on the other hand, reusable.

A cursor of all the data from the zipcode table may be very useful, but
it would be more useful for certain data processing if it held information
for only one state.

CURSOR c_zip (p_state IN zipcode.state%TYPE) IS

SELECT zip, city, state

FROM zipcode

WHERE state = p_state;

CSC Proprietary and Confidential

12

CURSORS WITH PARAMETERS

Cursor parameters make the cursor more reusable.

Cursor parameters can be assigned default values.

Parameterized cursors are also saying static


cursors that can passed parameter value when
cursor are opened.

The mode of the parameters can only be IN.

The c_zip cursor that was just declared is called as


follows:
OPEN c_zip (parameter_value)

CSC Proprietary and Confidential

13

Example
DECLARE
cursor c(no number) is select * from emp_information where emp_no
= no;
tmp emp_information%rowtype;
BEGIN
OPEN c(4);
FOR tmp IN c(4) LOOP
dbms_output.put_line('EMP_No: '||tmp.emp_no);
dbms_output.put_line('EMP_Name: '||tmp.emp_name);
dbms_output.put_line('EMP_Dept: '||tmp.emp_dept);
dbms_output.put_line('EMP_Salary:'||tmp.emp_salary);
END Loop;
CLOSE c;
END;
CSC Proprietary and Confidential

14

END
CSC Proprietary and Confidential

10/29/15

15
15

You might also like