Overview
Query statements may query multiple records, using cursors in stored procedures and functions to read the records in the query result set one by one. Some books call the cursor a cursor. The use of cursors includes declaring the cursor, opening the cursor, using the cursor, and closing the cursor. The cursor must be declared before the handler and declared after the variable and the condition. The cursor can be likened to a loop pointer within an array.
declaring Cursors
declare cursor_name cursor for select_statement;
Where the cursor_name parameter represents the name of the cursor; The select_statement parameter represents the contents of the SELECT statement.
Example of a declaration cursor
SELECT name, age FROM employe;
In the example above, a cursor named Cur_employee is declared. The name of the cursor is the CUR_EMPLOYEE;SELECT statement section that queries the value of the name and age fields from the employee table.
Open Cursor
open cursor_name;
using the cursor
fetch cursor_name into var_name[,var_name]...
Where the cursor_name parameter represents the name of the cursor, and the Var_name parameter indicates that the information queried by the SELECT statement in the cursor is stored in the parameter. Var_name must be well defined before the cursor is declared.
Close Cursor
close cursor_name;
After closing, you cannot use the cursor with fetch.
The use of the cursor in a stored procedure or function
目前,mysql中的光标只能在存储过程或函数中使用。
 
(a) cursor in MySQL