Cursors 13.6.6.2 Cursor DECLARE Statement 13.6.6.3 Cursor FETCH Statement 13.6.6.5 Restrictions on Server-Side Cursors 13.6.6.4 Cursor OPEN Statement 2805 Cursors are read only; you cannot use a cursor to update rows. In MySQL, a server-side cursor is materialized into an internal temporary table. Initially, this is a MEMORY  table, but is converted to a  MyISAM  table when its size exceeds the minimum value of the max_heap_table_size  and  tmp_table_size  system variables. The same restrictions apply to internal temporary tables created to hold the result set for a cursor as for other uses of internal temporary tables. See Section 8.4.4, “Internal Temporary Table Use in MySQL” . One limitation of the implementation is that for a large result set, retrieving its rows through a cursor might be slow. Server-side cursors are implemented in the C API using the  mysql_stmt_attr_set()  function. The same implementation is used for cursors in stored routines. A server-side cursor enables a result set to be generated on the server side, but not transferred to the client except for those rows that the client requests. For example, if a client executes a query but is only interested in the first row, the remaining rows are not transferred. This statement opens a previously declared cursor. For an example, see Section 13.6.6, “Cursors” . OPEN  cursor_name DECLARE  cursor_name  CURSOR FOR  select_statement FETCH [[NEXT] FROM]  cursor_name  INTO  var_name  [,  var_name ] ... This statement declares a cursor and associates it with a  SELECT  statement that retrieves the rows to be traversed by the cursor. To fetch the rows later, use a  FETCH  statement. The number of columns retrieved by the  SELECT  statement must match the number of output variables specified in the  FETCH  statement. The  SELECT  statement cannot have an  INTO  clause. Cursor declarations must appear before handler declarations and after variable and condition declarations. A stored program may contain multiple cursor declarations, but each cursor declared in a given block must have a unique name. For an example, see Section 13.6.6, “Cursors” . For information available through  SHOW  statements, it is possible in many cases to obtain equivalent information by using a cursor with an  INFORMATION_SCHEMA  table. This statement fetches the next row for the  SELECT  statement associated with the specified cursor (which must be open), and advances the cursor pointer. If a row exists, the fetched columns are stored in the named variables. The number of columns retrieved by the  SELECT  statement must match the number of output variables specified in the  FETCH  statement. If no more rows are available, a No Data condition occurs with SQLSTATE value  ’02000’ . To detect this condition, you can set up a handler for it (or for a  NOT FOUND  condition). For an example, see Section 13.6.6, “Cursors” . Be aware that another operation, such as a  SELECT  or another  FETCH , may also cause the handler to execute by raising the same condition. If it is necessary to distinguish which operation raised the condition, place the operation within its own  BEGIN ... END  block so that it can be associated with its own handler.