0

我第一次编写 MySQL 存储过程,我遇到了一个问题 - 我认为是处理程序代码。基本上,我希望这段代码更新表中的所有行pps_users,但由于某种原因,我在只获取了两行之后就达到了处理程序的“完成条件”。

我用 REPEAT 语法尝试了同样的事情并得到了同样的结果。如果我只运行游标查询,我正确地得到了我期望的 10,000 条记录,但是当我按原样运行整个事情时,我只在 1 或 2 条记录后点击了完成的代码。

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `changeNFLFavTeams`()
BEGIN
    DECLARE favNFLTeam varchar(100) DEFAULT "";    
    DECLARE favNCAATeam varchar(100) DEFAULT "";    
    DECLARE v_finished INTEGER DEFAULT 0;
    DECLARE user_id bigint(20);
    DECLARE fullNameOfTeam varchar(100) DEFAULT ""; 
    DECLARE update_favs CURSOR FOR select id, favorite_nfl_team from pps_users WHERE favorite_nfl_team is not null;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;    
    OPEN update_favs;


    updaterecord: LOOP
    FETCH update_favs INTO user_id, favNFLTeam;

        select user_id, favNFLTeam as "Test";

        if v_finished = 1
        then
            select "finished" as "finished";
            LEAVE updaterecord;
        end if;

        select full_name into fullNameOfTeam
                                from teams t 
                                inner join display_names dt on dt.entity_id = t.id 
                                and dt.entity_type = 'teams' 
                                and dt.first_name = favNFLTeam
                                and team_key like 'l.nfl.com%' LIMIT 1;

    select user_id, fullNameOfTeam AS "BeforeUpdate";

            IF fullNameOfTeam != ''
            THEN
               -- here for whatever_transformation_may_be_desired
            -- Find the Full name for the record they chose
                UPDATE pps_users p
                SET favorite_nfl_team = fullNameOfTeam
               WHERE user_id = p.id;
            ELSE
                SELECT 'A' AS 'A'; -- no op
            END IF;  


  end loop updaterecord;

  CLOSE update_favs;

END
4

1 回答 1

0

这是因为如果您的 SELECT full_name into fullNameOfTeam... 查询不返回任何行,那么它会将 v_finished 设置为 1。显然,这很早就发生了,并强制退出主循环。

关键是要认识到,CONTINUE HANDLER for NOT FOUND 并不单独适用于游标。

您应该使用自己的 CONTINUE 处理程序将辅助查询放入其自己的 BEGIN..END 块中,或者(更简单)在 SELECT full_name into fullNameOfTeam... 语句之后设置 v_finished = 0 。

于 2014-03-12T20:40:53.307 回答