The MySQL stored procedure uses the data queried by cursor in the cursor loop as part of the SQL statement.

Source: Internet
Author: User

This is not easy to describe in brief.

 

It is probably to update the data based on the query results, and the queried data has many rows.

The code below looks complicated, but it must be prepare as follows; otherwise, the update statement does not work.

 

drop PROCEDURE `UPGRADE_SKILL`//CREATE DEFINER=`root`@`%` PROCEDURE `UPGRADE_SKILL`()BEGIN DECLARE stopFlag, us_skill_id , s_id, ct int(11);     DECLARE s_uuid, str_sql varchar(255);           DECLARE cursor1 CURSOR FOR      SELECT us.skill_id , s.id, us.uuid FROM user_skill us, dragon_fx.skill s  WHERE s.old_id=us.skill_id and s.id>us.skill_id limit 1;          DECLARE CONTINUE HANDLER FOR NOT FOUND set stopFlag=1;           SET stopFlag=0;          OPEN cursor1;     SET ct =0;     REPEAT     SET ct=ct+1;            FETCH cursor1 INTO us_skill_id , s_id, s_uuid;            SET @s_id=s_id;            SET @s_uuid=s_uuid;            SET @str_sql=concat('UPDATE user_skill SET `skill_id` = ? WHERE `uuid` = ?;');            PREPARE stmt FROM @str_sql;            EXECUTE stmt USING @s_id,@s_uuid;     UNTIL stopFlag = 1 END REPEAT;     select ct;END;//call `UPGRADE_SKILL`//select * from user_skill where uuid='00012bd21f614b02b42450fc8ec5e9f6'//

You may think that an SQL statement can be used: Update dragon. user_skill us and dragon_fx.skill S.
Set us. skill_id = S. ID
Where S. old_id = us. skill_id and S. ID> us. skill_id

However, the execution is slow, intolerable, and does not seem to work at all (in confusion ).

 

 

 

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.