MySql cursor usage instance

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.