Cursor in SQL

Hello Friends…

In SQL , we are using cursors.. Cursor is a database object that retrieve data from a result set one row at a time.

We can use Cursor to retrieve one row of data from a set of data. and then we can perform operations like ,

eg: we can insert and update each row of data, we can perform calculations by taking each row.

cursor will perform like FOR loop.

First we need to declare cursor.From the above example screenshot,

DECLARE  CUR_APPROVE CURSOR STATIC FOR

CUR_APPROVE is the Cursor Name. Then select data from result set(data we are passing from input.contains bulk of data). This selected data from result set will be available in cursor. now we have set of rows in our cursor.

Then next step is -open cursor like below

OPEN CUR_APPROVE 

Then we can fetch each row of data from cursor and can perform operations on it

next step is closing cursor like below

CLOSE CUR_APPROVE 

Then Deallocate cursor like below,

DEALLOCATE CUR_APPROVE