Cursors

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 11

Cursors

Cursors are memory areas that allow You to allocate an area of memory and access the information retrieved from sql statement. Types of cursors 1. 2. Implicit cursor Explicit cursor

Cursors
1. Implicit cursor is automatically declared by oracle every time an sql statement is executed.the user will not be aware of this happening. Explicit cursor is defined by the program for any query that returns more than one row of data.

2.

Implicit Cursor
1. Cursor is automatically associated with every DML statement.

2. All Update and delete statements have cursors that identify the set of rows that will be affected by operation.

Explicit Cursor
1. Declaring the cursor :- This initializes the cursor into memory. 2. Opening cursor :- The previously declared cursor can now be opened;memory is alloted. 3. Fetching the cursor :- previously declared and opened cursor can now retrieve data;this is the process of fetching the cursor. 4. Closing the cursor :- Previously declared,opened, and fetched cursor must now be closed to release memory allocation.

Cursor Attributes
Cursor Attribute
%NOTFOUND

Syntax
Cursorname%NOTFOUND

Explanation
Returns true if the previous fetch didnt return row and false if it did. Returns true if the previous fetch returned a row ,and false if it did not. Records fetched from cursor at that point in time. Returns true if cursor is open,false if it is not.

%FOUND

Cursorname%FOUND

%ROWCOUNT

Cursorname%ROWCOUNT

%ISOPEN

Cursorname%ISOPEN

Cursors
declare vname varchar2(20); begin select fname into vname from student11; dbms_output.put_line('name is ' || vname); end;

Cursors
ORA-01422: exact fetch returns more than requested number of rows

Cursors
Declare Cursor student is Select fname from student11; Vname student11.fname%type; Begin Open student; Loop Fetch student into vname; Exit when student%notfound; Dbms_output.put_line('name is' || vname); End loop; Close student; end;

Cursors
Output
name is anit name is SUNIL PL/SQL procedure successfully completed.

Cursor For Loop


1. 2. 3. There is an alternative method of handling cursors.it is called cursor For loop. When using cursor for loop,the process of opening,fetching, and closing is handled implicitly. This makes the blocks much simpler to code and easier to maintain.

Cursor For Loop


declare cnt number; cursor c1 is select fname from student11; begin cnt:=0; for z in c1 loop cnt:=cnt+1; end loop; dbms_output.put_line(cnt); end;
3 PL/SQL procedure successfully completed.

You might also like