在存储过程中重新更新最后一个数字

时间:2013-02-23 01:41:46

标签: mysql

CREATE DEFINER=`root`@`localhost` PROCEDURE `SampleProc`( name_in INT)
BEGIN
DECLARE exit_loop BOOLEAN;
    DECLARE l_name INT;
            DECLARE X  INT;
     DECLARE Y INT;
     DECLARE c1id INT;
      DECLARE done INT DEFAULT FALSE;
        DECLARE cur CURSOR FOR SELECT PaperID FROM scrpd_paper_authors WHERE AuthorID=name_in;
     DECLARE cur1 CURSOR FOR SELECT AuthorID FROM scrpd_paper_authors WHERE PaperID=l_name;
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;
    OPEN cur;

     example_loop: LOOP
      FETCH cur INTO l_name;
      IF exit_loop THEN
    CLOSE cur;
    LEAVE example_loop;
    END IF;
    OPEN cur1;
      example_loop1:LOOP
       FETCH cur1 INTO c1id;

    UPDATE recipes_new SET co_auths = IF(LENGTH(co_auths), CONCAT(co_auths, ',', c1id), c1id) WHERE id = name_in;       

       IF exit_loop THEN
    CLOSE cur1;
    LEAVE example_loop1;
     END IF;
 END LOOP example_loop1;
 END LOOP example_loop;
END$$

我在sqlyog中创建了一个存储过程,我遇到的问题是当我看到更新完成后表中的结果是1,2,3,3而它应该是1,2,3最后一个条目正在重复。有什么猜测是什么问题?

recipes_new(My current result)       scrpd_paper_authors

id     co_auths                      PaperID     AuthorID

====    ========                      =======     ========
 1          1,1                          1            1
 2          2,2                          2            2
 3          3,4,4                        3            3
 4          3,4,5,6,7,7                  3            4
                                         4            5
                                         4            6
                                         4            7

你可以在recipes_new(我当前的结果)中看到我最后得到额外的数字。我的预期结果是

 1
 2
 3,4
 3,4,5,6,7

0 个答案:

没有答案