Oracle:Cursors

4,385
-1

Published on

Oracle:Cursors

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,385
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Oracle:Cursors

  1. 1. 1<br />Using ORACLE®<br />Triggers and Cursors<br />
  2. 2. 2<br />Triggers<br />A trigger is a stored procedure that defines an action that the database automatically initiates when some database related event such as INSERT , UPDATE or DELETE occurs.<br /> when occurs fires a <br />The main difference between a procedure and a trigger is that a procedure is executed explicitly from another block via a procedure call with arguments, while a trigger is executed implicitly when the triggering event like the occurrence of a DML-statement as INSERT ,UPDATE or DELETE occurs.<br />Also a trigger does not accept arguments.<br />The Automatic execution of triggers is known as firing of trigger.<br />
  3. 3. 3<br />Need and Types of Triggers<br />Need for triggers:<br />Maintaining complex integrity constraints.<br />Auditing the information in a table by recording the change.<br />Automatically signaling another program about an occurred event.<br />Enforcing complex business rules.<br />
  4. 4. 4<br />Need and Types of Triggers<br />Types of triggers:<br />Row trigger: Fired once for every row that is affected.<br />Statement trigger : Fired only once per statement regardless of the number of row affected.<br />Before trigger: Fired before the triggering event occurs.<br />After trigger: Fired after the triggering event occurs.<br />
  5. 5. 5<br />Syntax for Triggers<br />SYNTAX :<br />CREATE OR REPLACE TRIGGER trigger_name<br /> {BEFORE / AFTER } { INSERT / UPDATE / DELETE}<br />ON object_name<br /> FOR { EACH ROW / EACH STATEMENT}<br />BEGIN<br /> Statement1…<br /> …..<br /> …...<br />END;<br />
  6. 6. 6<br />Example for triggers<br />SYNTAX :<br />CREATE OR REPLACE TRIGGER trig<br /> AFTER DELETE<br />ON InfoTable<br /> FOR EACH ROW<br />BEGIN<br /> INSERT INTO InfoTable VALUES(:old name,NULL,NULL);<br />END;<br /> Using this trigger , whenever a row from the InfoTable will be deleted the trigger will be fired and the row containing the deleted row’s name will be inserted with other values as NULL which will help us identify later which rows were deleted.<br />
  7. 7. 7<br />NEED FOR CURSORS<br />We cannot use sub-programs of PL/SQL with a simple select statement to retrieve more than one row. If a select statement in a procedure returns more than one row ,Oracle returns an error message since PL/SQL requires a special compatibility to retrieve and process more than one row.<br /> Oracle processes procedure without error<br /> Oracle returns error.<br />SELECT statement in procedure returning one row<br />row<br />SELECT statement in procedure returning more than one row<br />row<br />row<br />row<br />
  8. 8. 8<br />CURSOR<br />A PL/SQL cursor is a mechanism that provides a way to select multiple rows of data from the database and then process each row individually inside a PL/SQL program. A cursor is basically an area of memory containing SQL statements and information for processing those statements.<br />A CURSOR is a pointer to the context area.<br /> CURSOR is <br /> pointing at <br /> this row1 CURSOR is <br /> pointing at<br /> this row2 now<br /> The cursor first points at row1 and once it is processed it then advances to row2 and so on.<br />row1<br />row1<br />row2<br />row2<br />row3<br />row3<br />row4<br />row4<br />
  9. 9. 9<br />Types of CURSORS<br />There are two types of cursors:<br />Implicit cursor: Created and used by Oracle for all DML and PL/SQL select statements including those returning only one row.<br />Explicit cursor : Created and used by the programmer for queries that return more than one row.<br />
  10. 10. 10<br />Types of CURSORS<br />SYNTAX:<br />DECLARE<br />CURSOR cursor_name <br />AS Cursor Declaration<br />SELECT statement;<br />-- Variable declaration if any;<br />BEGIN<br />OPEN cursor_name;<br />FETCH <cursor_name> INTO <record_list> Cursor Body<br />-- data fetched into active data set.<br />CLOSE cursor_name;<br />END;<br />
  11. 11. 11<br />EXAMPLE OF CURSOR<br />DECLARE<br />CURSOR cus AS SELECT * FROM InfoTable;<br />customer InfoTable%ROWTYPE; declare a rowtype variable<br />BEGIN<br />OPEN cus;<br />LOOP<br />FETCH cus into customer; Fetch the row into the variable and advance cursor.<br />DBMS_OUTPUT.PUT_LINE(‘name:’||customer.name);<br />DBMS_OUTPUT.PUT_LINE(‘age:’||customer.age);<br />DBMS_OUTPUT.PUT_LINE(‘phone:’||customer.phone);<br />EXIT WHEN cus%NOTFOUND; Returns true when all rows have been fetched.<br />END LOOP;<br />CLOSE cus;<br />END;<br />
  12. 12. THANK YOU<br />12<br />THANK YOU FOR VIEWING THIS PRESENTATION<br />FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING ,<br />please visit: <br />www.dataminingtools.net<br />

×