trigger and cursor program using sql

Download Trigger and cursor program using sql

If you can't read please download the document

Upload: sushil-mishra

Post on 17-May-2015

3.956 views

Category:

Documents


4 download

TRANSCRIPT

  • 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 samemechanics, an SQL procedure can also define a result set and return it directly tothe 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 asneeded. 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 databasemanagement system to actively control, monitor, and manage a group of tables

2. whenever an insert, update, or delete operation is performed. The statementsspecified in the SQL trigger are executed each time an SQL insert, update, ordelete 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 employeesfrom the emp table.Input Table SQL> SELECT ename, empno, sal FROM emp ORDER BY sal DESC;ENAMEEMPNOSAL---------- --------- -------- KING 7839 5000 SCOTT77883000 FORD 79023000 JONES 7566 2975 BLAKE76982850 CLARK77822450 ALLEN74991600 TURNER78441500 MILLER7934 1300 WARD7521 1250MARTIN 76541250 ADAMS 78761100 3. JAMES 7900950 SMITH 7369800PL/SQL Block-- available online in file sample2 DECLARECURSOR c1 isSELECT ename, empno, sal FROM empORDER BY sal DESC; -- start with highest paid employeemy_ename VARCHAR2(10);my_empno NUMBER(4);my_sal NUMBER(7,2);BEGINOPEN c1; FOR i IN 1..5 LOOPFETCH 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-------- -------- --------50007839 KING30007902 FORD3000 7788 SCOTT 29757566 JONES2850 7698 BLAKE TRIGGER PROGRAMThis 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_productAFTER insert On product FOR EACH ROW BEGININSERT INTO product_checkValues(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: MesageCurrent_Date------------------------------------------------------------ Before update, statement level26-Nov-2008Before update, row level26-Nov-2008After update, Row level 26-Nov-2008Before update, row level26-Nov-2008After update, Row level26-Nov-2008 After update, statement level26-Nov-2008