Mysql遊標阻止執行最後一次

DELIMITER $$

USE `you`$$

DROP PROCEDURE IF EXISTS `stockCursor`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `stockCursor`()
BEGIN  
    DECLARE change_id VARCHAR(40);
    DECLARE flag INT DEFAULT 0;
    DECLARE stock_cursor CURSOR FOR SELECT `order_id` FROM `ts_order` ;
       DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag=1;
        OPEN stock_cursor;
         /*循環*/
         REPEAT  
          FETCH stock_cursor INTO change_id;
           /*判斷防止多執行最後一次*/
           IF flag=0 THEN 
             INSERT INTO test(`order_id`) VALUES(change_id);
           END IF;
         UNTIL flag 
         END REPEAT;
    CLOSE stock_cursor ;
END$$

DELIMITER ;
相關文章
相關標籤/搜索