This tutorial explains how to use the FETCH statement to fetch the next row for a cursor in MySQL with syntax and examples.
The purpose of using a cursor, in most cases, is to retrieve the rows from your cursor so that some type of operation can be performed on the data. After declaring and opening your cursor, the next step is to use the FETCH statement to fetch rows from your cursor.
The syntax for the FETCH statement in MySQL is:
Parameters or Arguments
The name of the cursor that you wish to fetch rows.
The list of variables, comma separated, that you wish to store the cursor result set in.
Let's look at how to fetch the next row for a cursor using the FETCH statement in MySQL.
For example, you could have a cursor defined in MySQL as follows:
The command that would be used to fetch the data from this cursor is:
This would fetch the first site_id value into the variable called site_ID.
Below is a function that demonstrates how to use the FETCH statement.