0

我在 mysql 上有一个 sql 代码:

DELIMITER //
CREATE PROCEDURE proc()
main_block: BEGIN
 DECLARE is_finished BOOLEAN DEFAULT FALSE;
 DECLARE row_name VARCHAR(50) DEFAULT FALSE;
 DECLARE row_id INT DEFAULT FALSE;
 DECLARE cur CURSOR FOR SELECT `ID`, `Name` FROM `mytable`;
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET is_finished = TRUE;

 OPEN cur;
 my_loop: REPEAT 
  FETCH cur INTO row_id, row_name;

  IF row_id IS NULL THEN
   LEAVE main_block;
  END IF;

  SET @query = concat('SELECT `ID` INTO @msg_id from ', row_name, '_msgs');
  PREPARE run_query FROM @query;
  EXECUTE run_query;
  DEALLOCATE PREPARE run_query;

  ...

 UNTIL is_finished
 END REPEAT my_loop;
 CLOSE cur;
END;
END //

mytable包含三行,因此 Repeat-Until 应该工作三轮,但在EXECUTE run_query; DEALLOCATE PREPARE run_query; is_finishedget之后TRUE,循环在完成第一轮后结束。

如何禁用EXECUTEis_finished变量的影响?

4

0 回答 0