Use a cursor in the MySQL Stored Procedure

Source: Internet
Author: User
1 DELIMITER $
2
3 USE 'newhome _ db' $
4
5 drop procedure if exists 'get _ join_num '$
6
7 create procedure 'get _ join_num '()
8 BEGIN
9 DECLARE flag INT (11) DEFAULT 0;
10 DECLARE hd_id INT (11 );
11 DECLARE join_number INT (11 );
12 DECLARE my_cursor CURSOR
13 SELECT huodong_id, COUNT (*) FROM 'loupan _ collect 'WHERE category = 2 group by huodong_id;
14 declare continue handler for not found set flag = 1;
15 SET flag = 0;
16 OPEN my_cursor;
17 repeat fetch my_cursor INTO hd_id, join_number;
18 UPDATE 'loupan _ activity_basic 'SET join_num = join_number WHERE act_id = hd_id;
19 -- select hd_id, join_number;
20 UNTIL flag = 1 end repeat;
21 CLOSE my_cursor;
22 END $
23
24 DELIMITER;

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.