FETCH

A cursor is similar to a MySQL table or a view: it represents, within a procedure, a results set that is retrieved one row at a time using this MySQL statement.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

FETCH cursor INTO variable[, ...]

Explanation

A cursor is similar to a MySQL table or a view: it represents, within a procedure, a results set that is retrieved one row at a time using this MySQL statement. A cursor is established first with the MySQL statement, DECLARE. Then you use the MySQL statement, OPEN to initialize the cursor. The FETCH statement retrieves the next row of the cursor and places the data retrieved into one or more variables. There should the same number of variables as there are columns in the underlying SELECT statement of the cursor. Variables are given in a comma-separated list. Each execution of FETCH advances the pointer for the cursor by one row. Once all rows have been fetched, an SQLSTATE of 02000 is returned. You can tie a condition to this state through a DECLARE statement and end fetches based on the condition. Use the CLOSE statement statement to close a cursor.