Cursor is used to access the
records of a table one by one and can do theoperation.
Suppose you've a table named 'Person'as the
following:
ID | Name |
Age |
1 | aa | 20 |
2 | bb | 25 |
3 | cc | 30 |
4 | dd | 22 |
then, you can
create a CURSOR (with Declare, Open, Fetch and Close) asfollows for retrieving person name one by one:
DECLARE Names varchar(10);
DECLARE CursorTest CURSOR
FOR SELECT Name from Person;
OPEN CursorTest ;
FETCH CursorTest into Names ;
SELECT Names ;
CLOSE CursorTest ;
You can create it
in a stored procedure and can call the procedure to executeit. So the final result will be as follows: