Cursor
Cursor
Cursors
For every SQL statement execution certain area in memory is allocated. PL/SQL allow you to name this area. This private SQL area is called context area or cursor. Cursors allow the programmer to retrieve data from a table and perform actions on that data one row at a time. When a query returns multiple rows we have to create explicit cursor.
Cursor attributes
%NOTFOUND: It is a Boolean attribute, which evaluates to true, if the last fetch failed. i.e. when there are no rows left in the cursor to fetch. %FOUND: Boolean variable, which evaluates to true if the last fetch, succeeded. %ROWCOUNT: Its a numeric attribute, which returns number of rows fetched by the cursor so far. %ISOPEN: A Boolean variable, which evaluates to true if the cursor is opened otherwise to false.
There are two types of cursors implicit cursors and explicit cursors. Explicit Cursors Implicit cursors
For SQL queries returning single row PL/SQL declares implicit cursors. Implicit cursors are simple SELECT statements and are written in the BEGIN block (executable section) of the PL/SQL. Implicit cursors are easy to code, and they retrieve exactly one row. PL/SQL implicitly declares cursors for all DML statements. The most commonly raised exceptions here are NO_DATA_FOUND or TOO_MANY_ROWS. Syntax:
SELECT ename, sal INTO ena, esa FROM EMP WHERE EMPNO = 7844;
Count the number of rows affected by an update statement. Declare Num number(2); Begin Update student set grade=a where grade=b; Num:=sql%rowcount; DBMS_OUTPUT.PUT_LINE (rows effected : ||num); End;
Explicit Cursors
Explicit cursors are used in queries that return multiple rows. The set of rows fetched by a query is called active set. The size of the active set meets the search criteria in the select statement. Explicit cursor is declared in the DECLARE section of PL/SQL program.
Syntax: CURSOR <cursor-name> IS <select statement> Sample Code: DECLARE CURSOR emp_cur IS SELECT ename FROM EMP; BEGIN -----END;
Similarly user-defined explicit cursor needs to be opened, before reading the rows, after which it is closed Opening Cursor Syntax: OPEN <cursor-name>; Example : OPEN emp_cur;
Fetching from the cursor: To get the next row from the cursor we need to use fetch statement. Syntax: FETCH <cursor-name> INTO <variables>;
Closing the cursor: After retrieving all the rows from active set the cursor should be closed. CLOSE <cursor-name>;
declare Cursor c is select name from student where branch=cse; Mname student.name%type; Begin Open c; Loop Fetct c into mname; Exit when c%notfound; DBMS_OUTPUT.PUT_LINE (mname); End loop; Close c End;
Triggers
Trigger is the stored procedure that is automatically executed in response to certain events on a particular table or view in a database. It can be insert,delete,update
Types of triggers
Row trigger Statement trigger
A row trigger is fired for each row affected by a triggering statement. A statement trigger fired once for a triggering statement.
Create trigger del Before delete On branch_id For each row Begin Delete from student where branch_id=:old.branch_id; end;
Alter trigger del enable/disable; Alter table student disable all triggers; Drop trigger del ;
subprgrams
Procedures Functions
Procedure
Declare Num1 number(2); Num2 number(2); Mul number(2); Procedure multiply(num1 in number,num2 in number,mul out number) is Begin Mul:=num1*num2; End multiply; Begin Num1:=&num1; Num2:=&num2; multiply(num1,num2,mul); --calling DBMS_OUTPUT.PUT_LINE (mul); End;
Function
Declare Num1 number(2); Num2 number(2); Mul number(2); function multiply(num1 in number,num2 in number) Return number is Begin Mul:=num1*num2; Return(mul); End multiply; Begin Num1:=&num1; Num2:=&num2; Mul:=multiply(num1,num2); --calling DBMS_OUTPUT.PUT_LINE (mul); End;