Cursor Trigger
Cursor Trigger
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
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.
Syntax
Explanation
%NOTFOUND
%FOUND
cursor_name%FOUND
Cursor_name%ISOPEN
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
Explicit Cursor
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.
..
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;