You are on page 1of 7

Cursor

What is Cursor ?
In order for Oracle to process an SQL statement, it needs to create an area of memory known as
the context area; this will have the information needed to process the statement. This information
includes the number of rows processed by the statement, a pointer to the parsed representation of
the statement . In a query, the active set refers to the rows that will be returned.
A cursor is a handle, or pointer, to the context area. Through the cursor, a PL/SQL program can
control the context area and what happens to it as the statement is processed. Two important
features about the cursor are as follows:
1. Cursors allow you to fetch and process rows returned by a SELECT statement, one row at
a time.
2. A cursor is named so that it can be referenced.
What are the different 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. This declaration allows for the application to sequentially process each row of data
as it is returned by the cursor

What are the basic steps to create an explicit cursor ?


In order to use explicit cursors in PL/SQL code,(implicit cursors are automatically created )
we need to generally perform four steps. These four steps are:
Declare the cursor:- In this process ,the cursor is defined i.e, it is assigned a name and associated
with a SELECT statement, which is parsed (Parsing generally means in Oracle ensuring the
validity of a statement and determining how it is to be executed) .Any valid SELECT statement
can be used to define a cursor, including joins and statements with the UNION or MINUS
clause.
ii)Open the cursor:- In this step the ORACLE RDBMS executes the query associated with the
cursor and determines the qualified rows.
iii) Fetch rows from the cursor:-Is this step, one row is retrieved at a time from the active set. The
values for each row are returned to the PL/SQL subprogram environment. Rows are returned
one at a time.

iv)Close the cursor:- In this final step ,the cursor is closed which means that all resources
consumed by Oracle related to the cursor are released.
Create an explicit cursor to fetch all the enames table from the emp
DECLARE
Cursor empcursor IS
SELECT * FROM emp;
emprec emp%ROWTYPE;
BEGIN
OPEN empcursor;
LOOP
FETCH empcursor INTO emprec;
EXIT WHEN empcursor%NOTFOUND;
END LOOP;
CLOSE empcursor;
END;
Declaring a Cursor
Declaring a cursor defines the name of the cursor and associates it with a SELECT statement.
The first step is to Declare the Cursor with the following syntax:
CURSOR c_cursor_name IS select statement
Record Types
A record is a composite data structure, which means that it is composed of more than one
element. Records are very much like a row of a database table, but each element of the record
does not stand on its own. PL/SQL supports three kinds of records: (1) table-based, (2) cursorbased, (3) programmer-defined.
A table-based record is one whose structure is drawn from the list of columns in the table. A
cursor-based record is one whose structure matches the elements of a predefined cursor. To
create a table-based or cursor-based record, use the %ROWTYPE attribute.

<record_name> <table_name or cursor_name>%ROWTYPE


Opening a Cursor
The next step in controlling an explicit cursor is to open it. When the Open cursor statement is
processed, the following four actions will take place automatically:The variables (including bind
variables) in the WHERE clause are examined.
Based on the values of the variables, the active set is determined and the PL/SQL engine
executes the query for that cursor. Variables are examined at cursor open time only.
The PL/SQL engine identifies the active set of datathe rows from all involved tables that meet
the WHERE clause criteria.The active set pointer is set to the first row.
The syntax for opening a cursor is
OPEN cursor_name;
Fetching Rows in a Cursor :After the cursor has been declared and opened, you can then
retrieve data from the cursor. The process of getting the data from the cursor is referred to as
fetching the cursor. There are two methods of fetching a cursor, done with the following
command:
FETCH cursor_name INTO PL/SQL variables;
Or
FETCH cursor_name INTO PL/SQL record;
When the cursor is fetched, the following occurs:
The fetch command is used to retrieve one row at a time from the active set. This is generally
done inside a loop. The values of each row in the active set can then be stored into the
corresponding variables or PL/SQL record one at a time, performing operations on each one
successively.
After each FETCH, the active set pointer is moved forward to the next row. Thus, each fetch will
return successive rows of the active set, until the entire set is returned. The last FETCH will not
assign values to the output variables; they will still contain their prior values.
Closing a Cursor
Once all of the rows in the cursor have been processed (retrieved), the cursor should be closed.
This tells the PL/SQL engine that the program is finished with the cursor, and the resources
associated with it can be freed. The syntax for closing the cursor is

What are the different explicit cursor attributes?

Explicit Cursor Attributes


Cursor
Attribute

Syntax

Explanation

%NOTFOUND

cursor_name%NOTFOUND A Boolean attribute that returns TRUE


if the previous FETCH did not return a
row, and FALSE if it did.

%FOUND

cursor_name%FOUND

A Boolean attribute that returns TRUE


if the previous FETCH returned a row,
and FALSE if it did not.

%ROWCOUNT cursor_name%ROWCOUNT # of records fetched from a cursor at


that point in time.
%ISOPEN

Cursor_name%ISOPEN

A Boolean attribute that returns TRUE


if cursor is open, FALSE if it is not.

PROBLEM:1)Consider the two given tables EMP and DEPT of the default demo database of Oracle.Write
cursor declaration for the following:i)Cursor storing employees not located in CHICAGO.
ii) Number and name of each department with five or more employees.
Solution :
DECLARE
CURSOR c1 IS
SELECT empno,ename FROM emp
WHERE deptno IN
( SELECT deptno FROM dept WHERE loc <> CHICAGO)
.
ii) DECLARE
CURSOR c1 IS
select t1.deptno,dname, count(*) as "staff" from dept t1 ,(select deptno,count (*) "staff" from
emp group by deptno) t2 where t1.deptno=t2.deptno and "staff">=5
..
When a DML statement is executed ,where is the outcome of the statement saved?
When a DML statement is executed, the outcome of the statement is saved in four cursor
attributes. These are:
SQL%FOUND and SQL%NOTFOUND
SQL%ROWCOUNT
SQL%ISOPEN

How are implicit cursors different from explicit cursors?


Implicit Cursor

Explicit Cursor

1) Maintained internally by PL/SQL.


Opened and closed automatically when the
query is executed
2) The cursor attributes are prefixed with
SQL (e.g SQL%FOUND)

1) Defined with a name and is opened and


closed explicitly in a PL/SQL program

3)The cursor attributes %ISOPEN is


always FALSE

3)The %ISOPEN attribute holds the value


of the status of the cursor

2) The cursor attributes are prefixed with


the cursor name(e.g, %FOUND)

4) Only one row can be processed using the 4) Any number of rows can be processed.
SELECT INTO statement
Iterative routines are usually used to
process each row.

Trigger
What is trigger?
A trigger is a statement that the system execute automatically as a side effect of modification to
the database.
What is ECA model?
To design a trigger a trigger mechanism we must have meet the two requirements:
i) Specify when a trigger to be executed .This is broken up into an event that cause the trigger to
be checked and a condition that must be satisfied for trigger execution process
ii) Specify the action to be taken when the trigger execute
The above model of trigger creation is referred to as the event condition action model(ECA
model) for trigger.

What is Active database?


A database that has a set of associated trigger is called active database.
What are the different types of trigger?
Classification of trigger:i)Row level: The rule is to trigger separately for each tuple.
ii) Statement level:
This rule would be triggered for each triggering statement.
3) Before trigger:- This type of trigger execute the trigger action before the triggering statement.
iv) After trigger:- This type of trigger execute the triggering action after the triggering statement
is executed.
What are INSTEAD OF triggers?
The INSTEAD OF triggers are the triggers written specially for modifying views,which can not
be directly modified through SQL DML statements.These triggers are called INSTEAD OF
triggers because ,unlike other types of triggers,Oracle fires the trigger instead of executing the
triggering statement. The trigger performs update,insert,or delete operation directly on the
underlying tables.
By default ,INSTEAD OF triggers are activated for each row.
What is the syntax for creating a trigger?
Syntax:CREATE [ OR REPLACE]TRIGGER trigger_name
BEFORE|AFTER NSERT|UPDATE|DELETE
OF column_name,column_name,]
ON table_name
[FOR EACH ROW]
[WHEN condition]
BEGIN

..

Statement here

END;
A simple trigger creation problem:1* create table t4(a integer, b char(10))
SQL> /
Table created.
SQL> create table t5(c char(10),d integer);
Table created.
create trigger trig1
after insert on t4
for each row
when(new.a<10)
begin
insert into t5 values( :new.b, :new.a );
end;

You might also like