(12) mysql cursor, 12mysql cursor
Overview
The query statement may query multiple records and use the cursor in the stored procedure and function to read records in the query result set one by one. In some books, the cursor is called a cursor. The use of the cursor includes declaring the cursor, opening the cursor, using the cursor, and closing the cursor. The cursor must be declared before the handler and after the variables and conditions. The cursor can be analogous to the loop pointer in the array.
Declare cursor
declare cursor_name cursor for select_statement;
The cursor_name parameter indicates the name of the cursor, And the select_statement parameter indicates the content of the SELECT statement.
Example of declaring a cursor
DECLARE cur_employee SELECT name, age FROM employe;
In the above example, declare a cursor named cur_employee. The cursor name is cur_employee; the SELECT statement is used to query the value of the name and age fields from the employee table.
Open cursor
open cursor_name;
Use cursor
fetch cursor_name into var_name[,var_name]...
The cursor_name parameter indicates the name of the cursor. The var_name parameter indicates storing the information queried by the SELECT statement in the cursor into this parameter. Var_name must be defined before the cursor is declared.
Close the cursor
close cursor_name;
After being disabled, you cannot use FETCH to use the cursor.
Usage of the cursor in a stored procedure or function
Currently, the cursor in mysql can only be used in stored procedures or functions.