WINSEM2023-24 BCSE302P LO CH2023240502456 Reference Material III 28-03-2024 PLSQLCursors and Triggers
WINSEM2023-24 BCSE302P LO CH2023240502456 Reference Material III 28-03-2024 PLSQLCursors and Triggers
WINSEM2023-24 BCSE302P LO CH2023240502456 Reference Material III 28-03-2024 PLSQLCursors 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;
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
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
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
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
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
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