我是 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
,我看到的结果表明我观察到的问题。
关于这个错误的任何想法?