Tutorials
Oracle 9i
Sponsored Links
CURSOR
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. A cursor acts as a handle or pointer into the context area. A PL/SQL program controls the context area using the cursor. Cursor represents a structure in memory and is different from cursor variable.
When you declare a cursor, you get a pointer variable, which does not point any thing. When the cursor is opened, memory is allocated and the cursor structure is created. The cursor variable now points the cursor. When the cursor is closed the memory allocated for the cursor is released.
Cursors allow the programmer to retrieve data from a table and perform actions on that data one row at a time. There are two types of cursors implicit cursors and 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;
Note: Ename and sal are columns of the table EMP and ena and esa are the variables
used to store ename and sal fetched by the query.

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;
Processing multiple rows is similar to file processing. For processing a file you need to open it, process records and then close. Similarly user-defined explicit cursor needs to be opened, before reading the rows, after which it is closed. Like how file pointer marks current position in file processing, cursor marks the current position in the active set.
Opening Cursor
Syntax: OPEN <cursor-name>;
Example : OPEN emp_cur;
When a cursor is opened the active set is determined, the rows satisfying the where clause in the select statement are added to the active set. A pointer is established and points to the first row in the active set.
Fetching from the cursor: To get the next row from the cursor we need to use fetch statement.
Syntax: FETCH <cursor-name> INTO <variables>;
Example: FETCH emp_cur INTO ena;
FETCH statement retrieves one row at a time. Bulk collect clause need to be used to fetch more than one row at a time.
Closing the cursor: After retrieving all the rows from active set the cursor should be closed. Resources allocated for the cursor are now freed. Once the cursor is closed the execution of fetch statement will lead to errors.
CLOSE <cursor-name>;
Explicit Cursor Attributes
Every cursor defined by the user has 4 attributes. When appended to the cursor name these attributes let the user access useful information about the execution of a multirow query.
The attributes are:

In above example I wrote a separate fetch for each row, instead loop statement could be used here. Following example explains the usage of LOOP.
Using WHILE:
While LOOP can be used as shown in the following example for accessing the cursor values.
Example:

Fetch is used twice in the above example to make %FOUND available. See below example.

Sponsored Links
Using Cursor For Loop:
The cursor for Loop can be used to process multiple records. There are two benefits with cursor for Loop
1. It implicitly declares a %ROWTYPE variable, also uses it as LOOP index
2. Cursor For Loop itself opens a cursor, read records then closes the cursor automatically. Hence OPEN, FETCH and CLOSE statements are not necessary in it.
Example: 
emp_rec is automatically created variable of %ROWTYPE. We have not used OPEN, FETCH , and CLOSE in the above example as for cursor loop does it automatically. The above example can be rewritten as shown in the Fig , with less lines of code. It is called Implicit for Loop.

Deletion or Updation Using Cursor:
In all the previous examples I explained about how to retrieve data using cursors. Now we will see how to modify or delete rows in a table using cursors. In order to Update or Delete rows, the cursor must be defined with the FOR UPDATE clause. The Update or Delete statement must be declared with WHERE CURRENT OF
Following example updates comm of all employees with salary less than 2000 by adding 100 to existing comm.

| Notes on Cursors. |
| The notes are quite good :D |
| really it\'s very nice |
| :).it will be the best if you have also mentioned the internal diffrence between implicit and explicit cursor . |
| Nice Work ....... Short and simple |
| it helped me a lot.i did it without listening to classes |
| its very good explanation. and provide real time scenarios then its look very good. |
|
The way it was explained is really good. Thank u |
| very nice explanation is given |
| its very good explanation. and provide real time scenarios then its look very good. |
|
this is exactly what i needed....everthing is mentioned in its completeness and is to the point. thank a ton |
|
Very Nice. It Helps alot to the New Comer's. I hope and Prayer this kind of this will be going on from your kind side. I suggest you to go forward with the new & advance features of 9i as well as 10 g technology. Have a nice Day. |
| very nice . |
| It helps a lot for beginner like me. Thanks |
| really u have explained complex things in a very simple manner |
| Thanks, Good explanatoon it really helps us. |
|
any example for ref cursor ;D |
|
Its excellent . Gr8 job man. U can easily understand by reading this topics coz its written in simple language and most of all its with examples. Thanks man. Carry on the good work |
| Its good example for learners.It is very clear to understand. |
| it's very very nice , it's a greate site . it looks like user friendly. easy to understand without taking the course and joing to institution. The examples are given here is very good to gain knowledge on particular topic. |
| Awsum, Excellent it really helps me Keep it up. |
|
Hi everyone, This is an example of a great site. unfortunately, when we want to know more about some topic this informations is not enough. I have one doubt about who to know the number of rows of a cursor, for example, if we have the following example: open c_myCursor for select p.myName from partners p how can I know the number of rows afected to this cursor after this? Can anyone help me? Thansk. |
| ur detailed explainations helps a lot |
| VERY CLEARLY EXPLAINED.. THXS. |
| Really Appreciate |
| it is very good. provide some more examples thats is very much good |
| Examples are good,Its very easy to understand the beginners |
| Nice explanation . Pls explain about which tablespace that cursor is located?? |
| This site is really helpful.Provide us with some more examples and complex problems. |
| Very good explanation. Really refreshes the concepts at a glance. |
| please tell me querry to insert the sysdate i.e date of the system into a column 9c_date of data type date )of a table |
|
ITS EXCELLENT COURSE.......... I HAVE READ SOME BOOKS BUT I DIDNT UNDERSTAND CLEARLY. BY READING THIS I AM GETTING CLEARLY AND FULL KNOWLEDGE. |
|
the content of the article is very nice.. Very helpful indeed. Thanks |
|
excellent material ,fantastic, Provide us with some more examples and complex problems plz tell me a cursor which insert only the system time into table column thanks |
|
I'm just about to embark on my first PL/SQL course this article has give me a great insight into cursors which I knew nothing about. Supurbly explained Thanks |
| good work . nice explanations |
| It's realy a nice collection and verry eassy to understand...... |
|
Its a really amazing website Very helpful and almost all my questions are answered Thank u |
| Very simple and very clear explanations. Good job. Pls continue yourgood effort on advanced topics also. |
|
thanks friends for explaing of cursor indetail |
| Its was really very helpful.Thanks a lot |
| it was very simple and understandable to begineers |
|
sir/madam, thank you for providing such feature in site can u please send me all the contents with examples it would be very helpful, to me and my felow employees |
| Its really great help providing to any person unknown about trigger ,function etc.. |
|
Really it is fabolus notes that you provided on net.They really helped me. Thanks |
|
had lots of confusion about cursors.. all cleared. thankyou so much for the same. keep the good work going. regards |
| Its Good, here its clesrly explianed about theCursors which are very Basics in writing any Procedure |
| Excellent job in very easy and clear straight way |
| Simple and Excellent examples. Anyone could please ellaborate on WHERE CURRENT OF |
|
Its very useful for beginners. While reading itself,we can feel tat working with Orale |
|
Thanks for reminding me the oracle cursors. I was hopeless to find any good and simple help unless I got to your website.. Thanks. |
|
It is a very simple and good way to understand the meaning of Cursor. Pls every one read it and get the Knoledge about cursor Thanks |
| It is very nice and examples are fantastic. One can learn a lot from this. |
| It is very nice. Examples are simply great and understandable. Nice work people -- congratulation |
|
it's truly very nice. appreciate your work. you explained difficult things in a very good and simple way. thanks.... :) have a gr8 day... |
|
The explanation is very clear and precise. Makes it easy for beginers. Thanx.............. |
|
One of The Best material i have come across.. Its an excellent desciption in a very simple way. Thanks a lot.. |
|
Hi , It is Really Good Example; |
| very help ful for beginners.providing excellent examples with execution |
| its a great site for newcomers1 |
| its really very helpful |
| this is really good one...thank you very much ...but after emp_rec when i try to work on emp_rec its not displaying records..i am a beginner..this notes helping me lot...once again thank you very much. |
| its really very helpful for beginners |
|
very clear. i am beginner this notes helped me a lot |
|
Hi Guys.... The information given here on cursor is very useful... Its quite clearly specified... Thanks |
| very useful |
|
great work .... keep it up |
| A good tutorial. I have never Imagined i could be that way |
|
its really good.. very useful... |
| Good Work |
| After reading this article we can understand Cursors concept easily...Thanks |
| Its really excellent.. Please keep to post the important topics.... |
| nice explanation |
| Good explanation |
| this is very useful to me...... |
| I am not much into PL/SQL programming, however due to my project requirements I had to look onto Cursors. The tutorial has given a good insight. Thanks for the material |
| Very Gud explanations ....Thanks |
|
Hi, This is nice and simple tutorial. It helped me a lot.Thanks |
|
Hey Guys, Clear, concise and precise :) Thanks for sharing such a superb tutorial. |
| It is short, but gives detailed information. Good one to read. |
| Its really good one. |
| Please give an example of a procedure returning a cursor |
|
Nice information for beginners. continue with others topic |
| Excellent forum..Thankyou for sharing. |
| very nice excellent work keep it up |
| Great job.......thanks..........its very helpful....... |
| well done ... its really helpfull |
| Valuable |
| Nice. It was very helpful to me. Thank you |
|
It is really good for beginner's. It will help to clean and clear our idea for Cursor. Thanks a lot. |
|
It's Really Good. Thanks |
| Really Nice with good example |
| this is very simple to understand |
| Great Notes..thanks a lot for this education. |
| Its very nice covered all the concepts . Appriciated .. |
| it is a incredible explanation |
| very usefull to the debut learners,very nice.thanks for.. |
| Excellent job done great work. Really helpfull. |
| Easy to understand. Well done. |
| It was really helpful to clear the concept of cursor. |
| It's very useful for every one thanks............. |
| I am impressed with it nice yaar......... |