Cursor and Trigger
Cursor and Trigger
Context area:
Cursor:
Note: The set of rows the cursor holds is known as active set.
1. Implicit cursors: These are created by default when DML statements like,
INSERT, UPDATE, and DELETE statements are executed. They are also
created when a SELECT statement that returns just one row is executed.
EXAMPLE:
BEGIN
UPDATE customer1
IF SQL%NOTFOUND THEN
var_rows := SQL%ROWCOUNT;
END IF;
END;
Output:
CURSOR cur_customer IS
SELECT name, salary, age FROM customer;
CLOSE cur_customer;
EXAMPLE:
DECLARE
c_salary customer1.salary%type;
c_name customer1.name%type;
c_age customer1.age%type;
CURSOR cur_customer is
BEGIN
OPEN cur_customer;
LOOP
END LOOP;
CLOSE cur_customer;
END;
Output
Statement processed.
Alex 12000 20
Alex 12000 20
Alex 12000 20
Jhon 82000 21
Bob 32000 22
Ana 12500 20
Emily 17000 30
Ron 22000 28
Trigger
A database trigger is a stored program which is automatically fired or executed
when some events occur. A trigger can be executed in response to any of the
following events:
1. A database manipulation (DML) statement like DELETE, INSERT or
UPDATE.
2. A database definition (DDL) statement like CREATE, ALTER or DROP.
3. A database operation like SERVERERROR, LOGON, LOGOFF, STARTUP, or
SHUTDOWN.
create table employee(emp_id int, name varchar2(20), age int, salary int );
DECLARE
sal_diff number;
BEGIN
END;
/
update employee set salary = 8523000 where emp_id = 2;
Output
1 row(s) updated.
Old salary: 20000
New salary: 8523000
Salary difference: 8503000