You are on page 1of 3

29-Apr-15

What are Cursors?


DBMS

A cursor is a temporary work area created in


the system memory when a SQL statement is
executed

High-level Language Extension with Cursors

This area is used to store & manipulate the data

A cursor contains information on a select


statement and the rows of data accessed by it.

Types of cursors in PL/SQL




Implicit cursors

Explicit cursors

A cursor can process only one row at a time

The set of rows the cursor holds  active set.

Types of cursors in PL/SQL




Implicit cursors


These are created by default when DML statements


like, INSERT, UPDATE, and DELETE statements are
executed.

Both implicit and explicit cursors have the

same functionality, but they differ in the way

They are also created when a SELECT statement


that returns just one row is executed

they are accessed.

Types of cursors in PL/SQL




Explicit cursors


Created when executing a SELECT statement that

Cursor Attributes


Check the status of DML Operations




returns more than one row.




Cursor stores multiple records, but only one record

%FOUND  TRUE/ FALSE

%NOTFOUND  TRUE/ FALSE


%ROWCOUNT  Number
%ISOPEN  TRUE/ FALSE

can be processed at a time,

This is called as current row.

When a row is fetched, the current row position


moves to next row.

29-Apr-15

Cursor Attributes

Cursor Attributes

Attribute

Attribute

%FOUND

Return Value
TRUE, if the DML statements like INSERT,
DELETE and UPDATE affect at least one row
and SELECT .INTO statement return at
least one row.
FALSE, if DML statements like INSERT,
DELETE and UPDATE do not affect row and
SELECT.INTO statement do not return a
row.

Cursor Attributes
Attribute

Return Value

Return the number of rows affected by


%ROWCOUNT the DML operations INSERT, DELETE,
UPDATE, SELECT

Procedure to work with Cursor




Declare a cursor that defines a result set.

Open the cursor to establish the result set.

Fetch the data into local variables as


needed from the cursor, one row at a time.

Creating a Cursor


CURSOR <cursor_name> IS <statement>

Return Value

FALSE, if the DML statements like INSERT,


DELETE and UPDATE affect at least one row
and SELECT .INTO statement return at
%NOTFOU least one row.
ND
TRUE, if DML statements like INSERT, DELETE
and UPDATE do not affect row and
SELECT.INTO statement do not return a
row.

Close the cursor when done

Opening and Closing a Cursor




OPEN <cursor_name> ;

CLOSE <cursor_name>;

Example:
CURSOR

C1

SELECT

ENAME

IS

FROM

EMPLOYEE

WHERE

DESG='system_analyst';

29-Apr-15

Fetching Records Single Attribute




FETCH <cursor-name> INTO Variable;

Fetching Records Multiple Attributes


DECLARE ED EMPLOYEE%ROWTYPE ;
LOOP
FETCH C1 INTO ED;

LOOP

EXIT WHEN C1%NOTFOUND;

FETCH C1 INTO NAME;

NAME:=ED.ENAME;

EXIT WHEN C1%NOTFOUND;

ID:=ED.EID;

DBMS_OUTPUT.PUT_LINE('NAME:'||NAME);

BIRDATE:=ED.BDATE;
DBMS_OUTPUT.PUT_LINE(NAME||ID||BIRDATE);

END LOOP;

END LOOP;

Lab Exercise


Create an Employee table with necessary attributes

Increment the salary of an employee based on

Category

Condition

Increment

Manager

Experience > 5 yrs

Basic = + 10%
DA = + 2%; HR = + 3 %; CA = + 2%.

Manager

Experience < 5 yrs


Experience > 1 yrs

Basic = + 5% DA = + 1%; HR = + 1 %;
CA = + 1%.

Officer

Experience > 3 yrs

Basic = + 5% DA = + 1%;

Experience < 3 yrs

No Increment

Officer

Calculate the new NETPAY accordingly

You might also like