Cursors & Triggers
Cursors & Triggers
Cursors
• 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.
• For INSERT operations, the cursor holds the data that needs to be
inserted.
• For UPDATE and DELETE operations, the cursor identifies the rows
that would be affected.
• In PL/SQL, you can refer to the most recent implicit cursor
as the SQL cursor, which always has the attributes like
%FOUND, %ISOPEN, %NOTFOUND, and
%ROWCOUNT.
11
Cursors
• A cursor is a temp store of data.
Active set
13
Controlling Cursor
No
Yes
DECLARE
DECLARE OPEN
OPEN FETCH
FETCH EMPTY? CLOSE
CLOSE
14
Controlling Cursor…
Open the cursor.
Pointer
Cursor
Fetch a row from the cursor.
Pointer
Cursor
Continue until empty.
Pointer
Cursor
Close the cursor.
Cursor 15
25463
Data
12245
Create or replace procedure proc_test as
returned 55983
v_empid number; by
cursor 12524
Cursor cur_sample is Declare 98543
Select empid from employee
Cursor
where grade > 4;
Open cursor for use.
Begin
Loops round each value
open cur_sample;
loop returned by the cursor
fetch cur_sample into v_empid;
exit when cur_sample%notfound;
Stop
when not
update employee Place the value from the
more
set salary = salary + 500 cursor into the variable
records where empid = v_empid; v_empid
are found end loop;
End;
16
The %ISOPEN Attribute
• Fetch rows only when the cursor is open.
• Use the %ISOPEN cursor attribute before
performing a fetch to test whether the cursor is
open.
• Example
IF
IF NOT
NOT cur_sample%ISOPEN
cur_sample%ISOPEN THEN
THEN
OPEN
OPEN cur_sample;
cur_sample;
END
END IF;
IF;
LOOP
LOOP
FETCH
FETCH cur_sample...
cur_sample...
17
Cursors and Records
Process the rows of the active set conveniently
by fetching values into a PL/SQL RECORD.
Example
DECLARE
DECLARE
CURSOR
CURSOR emp_cursor
emp_cursor IS
IS
SELECT
SELECT empno, ename
empno, ename
FROM
FROM emp;
emp;
emp_record
emp_record emp_cursor%ROWTYPE;
emp_cursor%ROWTYPE;
BEGIN
BEGIN
OPEN
OPEN emp_cursor;
emp_cursor;
LOOP
LOOP
FETCH
FETCH emp_cursor
emp_cursor INTO
INTO emp_record;
emp_record;
...
...
18
Cursor FOR Loops
Syntax
FOR
FOR record_name
record_name IN
IN cursor_name
cursor_name LOOP
LOOP
statement1;
statement1;
statement2;
statement2;
.. .. ..
END
END LOOP;
LOOP;
19
Cursor FOR Loops: An Example
Retrieve employees one by one until no more are
left.
Example
DECLARE
DECLARE
CURSOR
CURSOR emp_cursor
emp_cursor IS
IS
SELECT
SELECT ename,
ename, deptno
deptno
FROM
FROM emp;
emp;
BEGIN
BEGIN
FOR
FOR emp_record
emp_record ININ emp_cursor
emp_cursor LOOP
LOOP
--
-- implicit
implicit open
open and
and implicit
implicit fetch
fetch occur
occur
IF
IF emp_record.deptno
emp_record.deptno == 3030 THEN
THEN
...
...
END
END LOOP;
LOOP; --
-- implicit
implicit close
close occurs
occurs
END;
END;
20
Example
• The following program will update the table and increase the salary of each customer
by 500 and use the SQL%ROWCOUNT attribute to determine the number of rows
affected −
DECLARE
total_rows number(2);
BEGIN
UPDATE customers SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
END;
/
• output
• 6 customers selected
• PL/SQL procedure successfully completed.
Explicit Cursors
• Explicit cursors are programmer-defined cursors for gaining more
control over the context area.
• An explicit cursor should be defined in the declaration section of
the PL/SQL Block.
• It is created on a SELECT Statement which returns more than one
row.
• The syntax for creating an explicit cursor is −
CURSOR cursor_name IS select_statement;
EMP_No: 2
EMP_Name: marks jems
EMP_Dept: Program Developer
EMP_Salary:38k
• Command "FOR EACH ROW" will specify the ROW level trigger.
• WHEN clause will specify the additional condition in which the
trigger needs to fire.
• If you want to query the table in the same trigger, then you
should use the AFTER keyword, because triggers can query the
table or change it again only after the initial changes are applied
and the table is back in a consistent state.
• The above trigger has been written in such a way that it will fire
before any DELETE or INSERT or UPDATE operation on the
table, but you can write your trigger on a single or multiple
operations, for example BEFORE DELETE, which will fire
whenever a record will be deleted using the DELETE operation
on the table.
Triggering a Trigger
• Consider DML operations on the CUSTOMERS table.
Old salary:
New salary: 7500
Salary difference:
• Because this is a new record, old salary is not available and the
above result comes as null.
• Consider one more DML operation on the CUSTOMERS table.
• The UPDATE statement will update an existing record in the table
−
• UPDATE customers SET salary = salary + 500 WHERE id = 2;