0
저는 MySQL을 처음 사용하는데 약간의 문제가 있습니다.내부 커서의 마지막 행이 두 번 반입됩니다.
2 개의 커서가 있고 다른 하나는 내부에있는 저장 프로 시저가 있습니다.
내부 커서의 마지막 행이 항상 두 번 반입된다는 문제가 있습니다. 이것은 바깥 쪽 커서를 반복 할 때마다 마지막 행이 내부 커서에 올 때마다 발생합니다. 여기
는 전체 저장 프로 시저입니다 : 당신이 볼 수 있듯이, 나는 모든FETCH
후 수신 된 데이터를 기록하고있어, 나는이를 통해 볼 수있는 결과를 내 관찰 된 문제를 나타냅니다
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;
.
버그에 대한 아이디어가 있습니까?
products_cursor_finished 경우 나는'사용하는 경우 = 1 그 다음엔 떠나라. ', 내가 떠나기 후에 아무 말도하지 않겠 니? 내말은, 뭐라구? – Ahmad
필자는'REPEAT'를'LOOP' 기반 루핑으로 대체했습니다. 예 : www.mysqltutorial.org/mysql-cursor/ – Ahmad
LEAVE가 현재 LOOP로 존재합니다. 링크 : http://dev.mysql.com/doc/refman/5.0/en/leave.html – krokodilko