The document discusses using cursors and triggers in SQL procedures. It describes how cursors allow defining and iterating through a result set row by row. Triggers allow running code automatically when data changes, such as on insert, update or delete operations. An example cursor program selects the top 5 paid employees from a table and inserts their data into another table. An example trigger inserts a record into a log table each time a row is updated in another table.
1. AIM:
TRIGGER AND CURSOR PROGRAM USING SQL
INTRODUCTION:
In SQL procedures, a cursor make it possible to define a result set (a set of data
rows) and perform complex logic on a row by row basis. By using the same
mechanics, an SQL procedure can also define a result set and return it directly to
the caller of the SQL procedure or to a client application.
A cursor can be viewed as a pointer to one row in a set of rows. The cursor can only
reference one row at a time, but can move to other rows of the result set as
needed.
To use cursors in SQL procedures, you need to do the following:
1)Declare a cursor that defines a result set.
2)Open the cursor to establish the result set.
3)Fetch the data into local variables as needed from the cursor, one row at a time.
4)Close the cursor when done.
The SQL CREATE TRIGGER statement provides a way for the database
management system to actively control, monitor, and manage a group of tables
2. whenever an insert, update, or delete operation is performed. The statements
specified in the SQL trigger are executed each time an SQL insert, update, or
delete operation is performed. An SQL trigger may call stored procedures or user-
defined functions to perform additional processing when the trigger is executed.
CURSOR PROGRAM:
The following PROGRAM uses a cursor to select the five highest paid employees
from the emp table.
Input Table
SQL> SELECT ename, empno, sal FROM emp ORDER BY sal DESC;
ENAME EMPNO SAL
---------- --------- --------
KING 7839 5000
SCOTT 7788 3000
FORD 7902 3000
JONES 7566 2975
BLAKE 7698 2850
CLARK 7782 2450
ALLEN 7499 1600
TURNER 7844 1500
MILLER 7934 1300
WARD 7521 1250
MARTIN 7654 1250
ADAMS 7876 1100
3. JAMES 7900 950
SMITH 7369 800
PL/SQL Block
-- available online in file 'sample2'
DECLARE
CURSOR c1 is
SELECT ename, empno, sal FROM emp
ORDER BY sal DESC; -- start with highest paid employee
my_ename VARCHAR2(10);
my_empno NUMBER(4);
my_sal NUMBER(7,2);
BEGIN
OPEN c1;
FOR i IN 1..5 LOOP
FETCH c1 INTO my_ename, my_empno, my_sal;
EXIT WHEN c1%NOTFOUND; /* in case the number requested */
/* is more than the total */
/* number of employees */
INSERT INTO temp VALUES (my_sal, my_empno, my_ename);
COMMIT;
END LOOP;
CLOSE c1;
END;
Output Table
SQL> SELECT * FROM temp ORDER BY col1 DESC;
4. NUM_COL1 NUM_COL2 CHAR_COL
-------- -------- --------
5000 7839 KING
3000 7902 FORD
3000 7788 SCOTT
2975 7566 JONES
2850 7698 BLAKE
TRIGGER PROGRAM
This trigger will insert a record into the table 'product_check' before a sql
update statement is executed, at the statement level.
CREATE or REPLACE TRIGGER After_Update_Row_product
AFTER
insert On product
FOR EACH ROW
BEGIN
INSERT INTO product_check
Values('After update, Row level',sysdate);
END;
/
Now lets execute a update statement on table product.
UPDATE PRODUCT SET unit_price = 800
WHERE product_id in (100,101);
Lets check the data in 'product_check' table to see the order in which the trigger is fired.
5. SELECT * FROM product_check;
Output:
Mesage Current_Date
------------------------------------------------------------
Before update, statement level 26-Nov-2008
Before update, row level 26-Nov-2008
After update, Row level 26-Nov-2008
Before update, row level 26-Nov-2008
After update, Row level 26-Nov-2008
After update, statement level 26-Nov-2008