WINSEM2023-24 BCSE302P LO CH2023240502456 Reference Material III 28-03-2024 PLSQLCursors and Triggers

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 53

Cursors and Triggers

G.Sukanya
Asst.Prof(SCOPE)
Cursors
Uses of Cursors
• Cursors in SQL are used to retrieve and manipulate data one row at a time and we are
often used them in situations where it is necessary to process the data row by row.
• Cursors are useful when working with large datasets or when performing complex
calculations on the data.
• Cursors allow us to perform operations such as updating, deleting, or inserting records
based on some condition or criteria.
• Cursors are especially useful when processing data from multiple tables where the
relationships are not straightforward.
• Cursors can be used for transaction processing when it is necessary to process
multiple operations as part of a single transaction.
• Cursors can have a performance impact on the database, especially when processing
large datasets.
• Cursors should be used carefully and only when necessary.
Steps to execute the cursor
Types of cursor
• IMPLICIT CURSOR: The cursor will be opened by Oracle
and Oracle only will process all the steps
• EXPLICIT CURSOR: The cursor will be opened by us and
all the steps will be defined by us.
Implicit Cursor Attributes
Implicit Cursor-Example
Account relation
SQL> select * from account;

ACCTNO BRANCHNAME BALANCE


---------- ---------- ----------
101 anna salai 2332
102 hal 1221
103 vandalur 4433
104 ring road 5445
105 vandalur 7764
Implicit Cursor
SQL> DECLARE
total_rows number(2);
BEGIN
UPDATE account
SET balance = balance + 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 || ' accounts selected ');
END IF;
END;
/
Implicit Cursor - output
Implicit Cursor
• Display the city of the customer pooja

SQL> DECLARE
2 c_city customer.custcity%TYPE;
3 BEGIN
4 SELECT custcity
5 INTO c_city
6 FROM customer
7 WHERE custname='pooja';
8
9 DBMS_OUTPUT.put_line (c_city);
10 END;
11 /
chennai

PL/SQL procedure successfully completed.


Implicit Cursor
Customer who lives in avadi [if the output is single row, it works,
else we have to use loops]

SQL> declare
2 c_custname customer.custname%type;
3 c_custst customer.custst%type;
4 c_custcity customer.custcity%type;
5 total_rows number(2);
6
7 begin
8 select custname,custst,custcity into c_custname,c_custst,c_custcity
9 from customer where custst='avadi';
10 total_rows := sql%rowcount;
11 dbms_output.put_line( total_rows || ' customer selected');
12 end;
13 /
1 customer selected

PL/SQL procedure successfully completed.


Implicit Cursor
Customer who lives in chennai

SQL> begin
2 for c_cust in (select * from customer where
custcity='chennai')
3 loop
4 dbms_output.put_line( c_cust.custname);
5 end loop;
6 end;
7 /
pooja
aryan

PL/SQL procedure successfully completed.


SQL> begin
2 for c_city in (select custcity from customer)
3 loop
4 dbms_output.put_line(c_city.custcity);
5 end loop;
6 end;
7 /
delhi
chennai
bangalore
delhi
bangalore
chennai

PL/SQL procedure successfully completed.


Implicit Cursor
Display the number of customers who lives in chennai

SQL> declare
2
3 c_cust number(2);
4
5 begin
6
7 select count(*) into c_cust from customer where custcity='chennai';
8
9 dbms_output.put_line( c_cust);
10
11 end;
12 /
2

PL/SQL procedure successfully completed.


Explicit Cursor Attributes
• To know the status of a cursor
Controlling cursors
Explicit cursor functions
Declaring the cursor example
Explicit cursor example
Cursor for loop
• This loop is very useful when all rows of the cursors are to be
processed.
for <record_index> in <cname> loop
<loop-body>;
end loop;
• <record_index> is a record variable that is implicitly declared by
PL/SQL. Its scope is the for loop, and it can not be accessed
outside the for loop.

27
Cursor for loop
• The loop terminates automatically when all rows of the
cursor have been fetched.
• There is no need to open, fetch, or close the curse, and
there is no need to declare the record into which the
cursor rows are to be fetched.

28
Cursor for loop example
declare
cursor c1 is
select cno, cname, city c1_rec
from customers, zipcodes No declare for the record
where customers.zip = zipcodes.zip; into which the cursor rows
begin are to be fetched
for c1_rec in c1 loop
dbms_output.put-line(‘Row number ’ || c1%rowcount || ‘> ‘ ||
c_rec.cno || ‘ ‘ || c1-rec.cname || ‘ ‘ || c1_rec.city);
end loop
end;

29
declare
Explicit Cursor
c_ano account.acctno%type;
CURSOR c_account is
SELECT acctno FROM account;
BEGIN
OPEN c_account;
LOOP
FETCH c_account into c_ano;
EXIT WHEN c_account%notfound;
dbms_output.put_line(c_ano);
END LOOP;
CLOSE c_account;
END;
/
Explicit Cursor
Explicit Cursor – display all tuples
declare
c_ano account.acctno%type;
c_bname account.branchname%type;
c_balance account.balance%type;
CURSOR c_account is
SELECT acctno, balance, branchname FROM account;
BEGIN
OPEN c_account;
LOOP
FETCH c_account into c_ano, c_balance, c_bname;
EXIT WHEN c_account%notfound;
dbms_output.put_line(c_ano||' '||c_balance||' '||c_bname);
END LOOP;
CLOSE c_account;
END;
/
Explicit Cursor
List all account details with >2000 balance
SQL> declare
2 c_ano account.acctno%type;
3 c_bname account.branchname%type;
4 c_balance account.balance%type;
5 CURSOR c_account is
6 SELECT acctno, balance, branchname FROM account where balance>2000;
7 BEGIN
8 OPEN c_account;
9 LOOP
10 FETCH c_account into c_ano, c_balance, c_bname;
11 EXIT WHEN c_account%notfound;
12 dbms_output.put_line(c_ano||' '||c_balance||' '||c_bname);
13 END LOOP;
14 CLOSE c_account;
15 END;
16 /
101 2332 anna salai
103 4433 vandalur
104 5445 ring road
105 7764 vandalur

PL/SQL procedure successfully completed.


List all account details with >2000 balance
Student table

SQL> select * from student;

REGNO NAME COURSE MARKS


---------- -------- -------- ----------
111 sai dbms 88
112 deepu iwp 76
111 sai iwp 78
112 deepu dbms 87
Display grades – Explicit cursor
SQL> declare
2 c_regno student.regno%type;
3 c_name student.name%type;
4 c_course student.course%type;
5 c_marks student.marks%type;
6 grade char(2);
7 CURSOR c_student is
8 SELECT regno,name,course,marks FROM student;
9 BEGIN
10 OPEN c_student;
11 LOOP
12 FETCH c_student into c_regno, c_name, c_course, c_marks;
13 EXIT WHEN c_student%notfound;
14 if(c_marks>=80) then
15 grade:='s';
Display grades – Explicit cursor
16 else if((c_marks<=79) and (c_marks>=70)) then
17 grade:='a';
18 end if;
19 end if;
20 dbms_output.put_line(c_regno||' '||c_name||' '||c_course||' '||
grade);
21 END LOOP;
22 CLOSE c_student;
23 END;
24 /
111 sai dbms s
112 deepu iwp a
111 sai iwp a
112 deepu dbms s

PL/SQL procedure successfully completed.


Display grades – Explicit cursor
List the name of the customer with the maximum
loan amount
SQL> declare
2 cname borrower.custname%type;
3 cursor cur_cname is select custname from borrower where
loanno=(select loanno from loan where amount=(select max(amount) from
loan));
4 begin
5 open cur_cname;
6 loop
7 fetch cur_cname into cname;
8 exit when cur_cname%notfound;
9 dbms_output.put_line(cname);
10 end loop;
11 close cur_cname;
12 end;
13 /
aryan
List the name of the customer with the loan
amount
SQL> declare
2 cname borrower.custname%type;
3 amt loan.amount%type;
4 cursor cur_cnamt is select custname, amount from borrower natural join loan;
5 begin
6 open cur_cnamt;
7 loop
8 fetch cur_cnamt into cname, amt;
9 exit when cur_cnamt%notfound;
10 dbms_output.put_line(cname||' '||amt);
11 end loop;
12 close cur_cnamt;
13 end;
14 /
sai 300459
seetha 300459
aryan 232221
aryan 665544
anaya 211222

PL/SQL procedure successfully completed.


Triggers
• Triggers are stored PL/SQL units that automatically
execute ("fire") in response to specified events.
• A trigger is a PL/SQL unit that is stored in the database
and (if it is in the enabled state) automatically executes
("fires") in response to a specified event.
• The triggering event may be one of insert, delete, or
update(DML or DDL or DB opeartion)
Triggers
Syntax for triggers
Triggers
• referencing specifies correlation names that can be used to
refer to the old and new values of the row components
that are being affected by the trigger
• for each row designates the trigger to be a row trigger, i.e.,
the trigger is fired once for each row that is affected by the
triggering event and meets the optional trigger constraint
defined in the when clause.
• when specifies the trigger restriction.

47
• CREATE OR REPLACE TRIGGER display_salary_changes
• BEFORE DELETE OR INSERT OR UPDATE ON customers
• FOR EACH ROW
• WHEN (NEW.ID > 0)
• DECLARE
• sal_diff number;
• BEGIN
• sal_diff := :NEW.salary - :OLD.salary;
• dbms_output.put_line('Old salary: ' || :OLD.salary);
• dbms_output.put_line('New salary: ' || :NEW.salary);
• dbms_output.put_line('Salary difference: ' || sal_diff);
• END;
• /

48
49
Triggers
Triggers-Exmple
– Create a row level trigger for DML statements to display the
salary diffrence between the old and new value
OUTPUT
Triggering a Trigger

You might also like