What are the different steps in using MySQL cursor?

MySQLMySQLi Database

Followings are the different steps in using MySQL cursor −

  • DECLARATION − First of all we must have to declare a cursor by using a DECLARE statement. The cursor declaration must be after the variable declaration. The syntax for declaring MySQL cursor can be as follows −

DECLARE cursor_name CURSOR FOR SELECT-statement;
  • OPENING − Next, we need to open the cursor and it can be open by the OPEN statement. Actually, the OPEN statement initializes the result set for the cursor hence we must have to call OPEN statement before fetching rows from the result set. The syntax for opening MySQL cursor can be as follows −

OPEN cursor_name;
  • FETCHING the rows − Now, after opening the cursor, we need to use FETCH statement to retrieve the next row pointed by the cursor and move the cursor to the next row in the result set. The syntax for fetching MySQL cursor can be as follows −

FETCH cursor_name INTO variables list;
  • CLOSING − Finally we will close the cursor by a CLOSE statement to deactivate the cursor and release the memory associated with it. The syntax for closing MySQL cursor can be as follows −

CLOSE cursor_name;
raja
Published on 21-Feb-2018 15:48:06
Advertisements