mysql存储过程双重循环示例

BEGIN
    DECLARE menu_id VARCHAR(36);
    DECLARE done INT DEFAULT FALSE;
    DECLARE cursor_rule CURSOR FOR SELECT  s.id  FROM d_menu_type s ;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
            OPEN cursor_rule;
            l : LOOP
            FETCH cursor_rule INTO menu_id;
                IF done THEN 
                         LEAVE l;
                END IF;
                                    BEGIN
                                                            DECLARE property_id VARCHAR(36);
                                                            DECLARE done2 INT DEFAULT FALSE;
                                                            DECLARE cursor_rule2 CURSOR FOR SELECT  property_id FROM d_menu_property l WHERE l.menu_id=menu_id;
                                                            DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = TRUE;  
                                                            OPEN cursor_rule2;
                                                            l2 : LOOP
                                                            FETCH cursor_rule2 INTO property_id;
                                                            IF done2 THEN 
                                                                         LEAVE l2;
                                                            END IF;
                                                                
                                                            END LOOP;
                                                            CLOSE cursor_rule2;
                                    END;         
            END LOOP;
            CLOSE cursor_rule;
END

 

posted @ 2017-08-10 17:23  一曲秋殇  阅读(876)  评论(0编辑  收藏  举报