0% found this document useful (0 votes)
58 views

Cursor and Triggers

1) A cursor is a temporary work area created in memory when a DML statement is executed that holds the results of a query. It can hold multiple rows but can only process one row at a time. 2) There are four cursor attributes - %FOUND, %NOTFOUND, %ROWCOUNT, and %ISOPEN - that provide information about the cursor execution. 3) Triggers are blocks of PL/SQL code that execute automatically based on events like DML statements. They contain a triggering event, optional restriction, and trigger action body.

Uploaded by

Nitish Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Cursor and Triggers

1) A cursor is a temporary work area created in memory when a DML statement is executed that holds the results of a query. It can hold multiple rows but can only process one row at a time. 2) There are four cursor attributes - %FOUND, %NOTFOUND, %ROWCOUNT, and %ISOPEN - that provide information about the cursor execution. 3) Triggers are blocks of PL/SQL code that execute automatically based on events like DML statements. They contain a triggering event, optional restriction, and trigger action body.

Uploaded by

Nitish Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No: 09

Aim : Creating Cursor.

A Cursor is a temporary work area created in the memory of the system when a
DML statement is executed.
For Example, if the user wants data from the database, we execute a query in SQL
as
SELECT eid, ename FROM Employees.
A Cursor can hold more than one row. The set of all these rows that the cursor
holds is
called an active set. But... A Cursor can process only one row at a time.
There are four attributes used with Cursors :
1) %FOUND
2) %NOTFOUND
3) %ROWCOUNT
4) %ISOPEN
When these attributes are added to the cursor variable, they return some
information about the
execution of the SQL Statement.
1) %FOUND
As the cursor processes one row at a time, if the cursor variable is appended with
%FOUND
if the row was fetched successfully---It returns True
Otherwise ----It returns False
Note : Before the first fetch from an open cursor, %FOUND returns NULL
2. %ROWCOUNT: It returns the number of records fetched from the cursor at that
particular instance of time.
Note :
When a cursor is opened, %ROWCOUNT becomes zero . This means before the
first fetch from an open cursor, %ROWCOUNT returns 0.
3. %ISOPEN: If the cursor is open-----It returns True
Otherwise ----It returns False
Two Types of Cursors :
1) Implicit Cursors : Implicit Cursors are created by default by the system when
any DML statement gets executed.
2) Explicit Cursors : Explicit cursors must be created by us when we execute a
SELECT statement that returns one or more rows.
Uses of Cursors:
1. We can perform Row wise validation
2. Operations on each row
Write PL/SQL program to display id, name and address of each customer
using cursors
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;
/
When the above code is executed at the SQL prompt, it produces the following
result −
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP
PL/SQL procedure successfully completed.
Experiment No: 10
Aim : Creating packages and Triggers
Triggers in oracle are blocks of PL/SQL code which oracle engine can execute
automatically based on some action or event.
Triggers are automatically and repeatedly called upon by oracle engine on
satisfying certain condition. If triggers are activated then they are executed
implicitly by oracle engine.
Whenever a trigger is created, it contains the following three sequential parts:
1. Triggering Event or Statement: The statements due to which a trigger occurs.
2. Trigger Restriction: The condition or any limitation applied on the trigger. If
condition is TRUE then trigger fires. Not all triggers has conditions.

3. Trigger Action: The body containing the executable statements that is to be


executed when trigger fires.

Types of Triggers

1) Level Triggers

a) ROW LEVEL TRIGGERS: It fires for every record that got affected. It always
use a FOR EACH ROW clause

b) STATEMENT LEVEL TRIGGERS: It fires once for each statement that is


executed.

2) Event Triggers

a) DDL EVENT TRIGGER: It fires with the execution of every DDL


statement(CREATE, ALTER, DROP, TRUNCATE).
b) DML EVENT TRIGGER: It fires with the execution of every DML
statement(INSERT, UPDATE, DELETE).

c) DATABASE EVENT TRIGGER: It fires with the execution of every database


operation which can be LOGON, LOGOFF, SHUTDOWN, SERVERERROR etc.

3) Timing Triggers

a) BEFORE TRIGGER: It fires before executing DML statement.

b) AFTER TRIGGER: It fires after executing DML statement.

Example : Create a trigger that fires after insert of every new row in Stu table. The
trigger automatically updates total.

create table Stu(id int, subj1 int,subj2 int,subj3 int,total int);

Table created.

Insert values
insert into Stu(id,subj1,subj2,subj3) values(1,30,25,62);

ID SUBJ1 SUBJ2 SUBJ3


1 30 25 62

Output:
select * from Stu;
ID SUBJ1 SUBJ2 SUBJ3 TOTAL

1 30 25 62
Create Trigger Command:
create or replace trigger stud_marks
after INSERT
on
Stu
for each row
begin
update Stu set total = subj1 + subj2 + subj3;
end;
/

Output:
select * from Stu;
ID SUBJ1 SUBJ2 SUBJ3 TOTAL
1 30 25 62 117

You might also like