内部游标的最后一行获取两次

时间:2013-10-12 21:45:28

标签: mysql sql cursor

我是MySQL的新手,我有一个小问题。

我有一个存储过程,它有2个游标,一个在另一个游标内。

问题是内部游标的最后一行总是被提取两次。对于外部游标的每次迭代,每次最后一行进入内部游标时都会发生这种情况。

以下是完整的存储过程:

CREATE PROCEDURE MAP_TITLES_TO_SRC_CATEGORIES()
BEGIN
    Block1:BEGIN
        DECLARE matched_titles_category_id INTEGER DEFAULT 0;
        DECLARE tmp_genre_category_id INTEGER DEFAULT 0;
        DECLARE index_wanted INT Default 0;
        DECLARE genre_string VARCHAR(255);
        SET matched_titles_category_id = (SELECT category_id FROM oc_category_description WHERE name='matched_titles' LIMIT 1);
        Block2:BEGIN
            DECLARE src_cursor_finished INTEGER DEFAULT 0;
            DECLARE src_cursor_src_code_value varchar(9) DEFAULT "";
            DECLARE src_cursor_genres_value varchar(100) DEFAULT "";
            DECLARE src_cursor CURSOR FOR SELECT it.src_id, it.Genres FROM src_table it order by it.Title asc;
            DECLARE CONTINUE HANDLER FOR NOT FOUND SET src_cursor_finished = 1;
            OPEN src_cursor;
                REPEAT
                    FETCH src_cursor INTO src_cursor_src_code_value, src_cursor_genres_value;
                    INSERT INTO src_log (log_entry) VALUES (CONCAT('Cursor #1 populated with :: src_cursor_src_code_value: ',src_cursor_src_code_value,' & src_cursor_genres_value: ',src_cursor_genres_value));
                    Block3:BEGIN
                        DECLARE products_cursor_finished INTEGER DEFAULT 0;
                        DECLARE products_cursor_id_value INTEGER DEFAULT 0;
                        DECLARE products_cursor_isbn_value varchar(9) DEFAULT "";
                        DECLARE products_cursor CURSOR FOR SELECT prod.product_id, prod.isbn FROM oc_product prod where prod.isbn !='' and prod.sku='1';
                        DECLARE CONTINUE HANDLER FOR NOT FOUND SET products_cursor_finished = 1;
                        OPEN products_cursor;
                            REPEAT
                                FETCH products_cursor INTO products_cursor_id_value, products_cursor_isbn_value;
                                INSERT INTO src_log (log_entry) VALUES (CONCAT('Cursor #2 populated with :: products_cursor_id_value: ',products_cursor_id_value,' & products_cursor_isbn_value: ',products_cursor_isbn_value));
                                SET index_wanted = 0;
                                IF products_cursor_isbn_value = src_cursor_src_code_value THEN
                                    INSERT INTO src_log (log_entry) VALUES (CONCAT('match entry for prod ',products_cursor_id_value,' in match cat id ',matched_titles_category_id,' BEGIN'));
                                    INSERT INTO oc_product_to_category VALUES (products_cursor_id_value, matched_titles_category_id);
                                    INSERT INTO src_log (log_entry) VALUES (CONCAT('match entry for prod ',products_cursor_id_value,' in match cat id ',matched_titles_category_id,' END'));
                                    genres_loop:LOOP
                                        SET index_wanted=index_wanted+1;
                                        SET genre_string=SPLIT_STR(src_cursor_genres_value,',',index_wanted);
                                        IF genre_string='' THEN
                                            LEAVE genres_loop;
                                        END IF;
                                        SET tmp_genre_category_id = (SELECT category_id FROM oc_category_description WHERE name = genre_string LIMIT 1);
                                        INSERT INTO src_log (log_entry) VALUES (CONCAT('genre entry for prod ',products_cursor_id_value,' and genre cat ID ',tmp_genre_category_id,' BEGIN'));
                                        INSERT INTO oc_product_to_category VALUES (products_cursor_id_value, tmp_genre_category_id);
                                        INSERT INTO src_log (log_entry) VALUES (CONCAT('genre entry for prod ',products_cursor_id_value,' and genre cat ID ',tmp_genre_category_id,' END'));
                                    END LOOP genres_loop;
                                END IF;
                            Until products_cursor_finished END REPEAT;
                        CLOSE products_cursor;
                    END Block3;
                UNTIL src_cursor_finished END REPEAT;
            CLOSE src_cursor;
        END Block2;
    END Block1;
END;

正如您所看到的,我记录了每FETCH后收到的数据,我看到的结果表明了我观察到的问题。

关于这个bug的任何想法?

1 个答案:

答案 0 :(得分:1)

必须在FETCH命令后立即进行测试src_cursor_finished 但代码尝试从游标中获取,然后执行许多操作(不检查获取是否成功),然后在UNTIL语句中检查最后的条件:

DECLARE CONTINUE HANDLER FOR NOT FOUND SET src_cursor_finished = 1;
OPEN src_cursor;
  REPEAT
     FETCH products_cursor INTO products_cursor_id_value, products_cursor_isbn_value;

     -- The condition must be tested  HERE: 
     --    IF products_cursor_finished <> 1 THEN do something
     --        or even better:
     --    IF products_cursor_finished = 1 THEN LEAVE;

     ................
     ..............
     ..........
     ............
     ...................
  Until products_cursor_finished END REPEAT;