The whole process of using the mysql cursor is:
1. Create a cursor
Copy codeThe Code is as follows: DECLARE calc_bonus cursor for select id, salary, commission FROM employees;
2. Open the cursor
Copy codeThe Code is as follows: OPEN calc_bonus;
3. Use a cursor
Copy codeThe Code is as follows: FETCH calc_bonus INTO re_id, re_salary, re_comm;
4. Close the cursor
Copy codeThe Code is as follows: CLOSE calc_bonus;
The instance code is as follows:
Copy codeThe Code is as follows:
Begin
Declare temp_user_id int default null;
Declare stop int default 0;
# Declare a cursor
Declare temp_cur cursor for select f_user_id from table_test where f_user_id = 1;
# Exception Handling for declaring a cursor
Declare continue handler for sqlstate '200' set stop = 1;
Open temp_cur;
Fetch temp_cur into temp_user_id;
# Determine whether the cursor has reached the end
While stop <> 1 do
# Various judgments
# Reading data from the next row
Fetch temp_cur into temp_user_id;
# Loop end
End while;
# Closing a cursor
Close temp_cur;
End