Professional Documents
Culture Documents
A CURSOR is a name given to the Active Set or Context Area or Temporary Memory Area which stores result of SELECT STATEMENT. Using CURSOR we can fetch one by one record from Context Area and process the record. Using CURSOR we can do row processing. CURSORS are of 2 types: EXPLICIT CURSORS: Follow below steps to use Explicit Cursors in PL/SQL program: - DECLARE CURSOR - OPEN CURSOR - FETCH RECORDS FROM CURSOR - CLOSE CURSOR DECLARE CURSOR CURSOR <NAME> IS SELECT STATEMENT; e.g: CURSOR C1 IS SELECT * FROM EMP; EXPLICIT CURSORS: Cursors declared by user. IMPLICIT CURSORS: Cursors declared by System.
When CURSOR is open following things happens: - SELECT Statement is submitted to oracle server. - Oracle Server creates Context Area. - Records returned by the SELECT Statement are loaded into Context Area. - C1 points to the beginning of Context Area. FETCH RECORDS FROM CURSOR FETCH Statement is to fetch records from Context Area. FETCH <CURSORNAME> INTO <VARIABLES>
e.g:
The statement fetches records from C1 and is assigned to the variables X,Y,Z, . A FETCH Statement fetches only one record at a time but to process multiple records keep the fetch statement inside a loop. CLOSING CURSOR Once processing is completed then close CURSOR. CLOSE <CURSORNAME> e.g: CLOSE C1;
CURSOR ATRRIBUTES
%FOUND, %NOTFOUND, %ROWCOUNT, %ISOPEN
USING WHILE LOOP BEGIN OPEN C1; FETCH C1 IN R; WHILE (C1%FOUND) LOOP DBMS_OUTPUT.PUT_LINE(R.ENAME|| ||R.SAL);
FETCH C1 IN R; END LOOP; CLOSE C1; END; Here fetch statement is required 2 times, one to start the while loop and second to continue the while loop.