mysql while嵌套

 
 
#薪酬明细
DELETE FROM `wos_salary`.`ihr_salary_feedback_dep_detail` WHERE `dep_id` = 1697;
DROP PROCEDURE IF EXISTS jyf_test;
DELIMITER ;;
CREATE PROCEDURE jyf_test()
BEGIN
    DECLARE a INT UNSIGNED DEFAULT 1;
        DECLARE b INT UNSIGNED DEFAULT 1;
        DECLARE m INT UNSIGNED DEFAULT 1;
    DECLARE tmp VARCHAR(255);
    DECLARE t_item_name VARCHAR(255);
        DECLARE t_item_id INT UNSIGNED DEFAULT 1;
        WHILE m <= 12 DO
        if m<=9 THEN                    
            SET tmp=CONCAT(2020-0,m);
        ELSE
            SET tmp=CONCAT(2020-,m);
        end if; 
                WHILE a <= 24 DO
                SET b=a-1;
                SELECT `item_name` INTO t_item_name  FROM `wos_salary`.`ihr_salary_feedback_item` LIMIT b,1;#循环取数据,要加limit        
            SELECT `item_id` INTO t_item_id  FROM `wos_salary`.`ihr_salary_feedback_item` LIMIT b,1;        
                IF MOD(a,2)=0 THEN
                        INSERT INTO `wos_salary`.`ihr_salary_feedback_dep_detail`(`period`, `dep_id`, `item_id`, `item_name`, `item_process_category`, `prev_item_amount`, `item_amount`, `item_status`, `sequential_rate`, `create_time`)
                        SELECT tmp,1697,t_item_id,t_item_name,1,-a,a*100,1,a+1,1606889089 FROM DUAL;
                ELSE
                        INSERT INTO `wos_salary`.`ihr_salary_feedback_dep_detail`(`period`, `dep_id`, `item_id`, `item_name`, `item_process_category`, `prev_item_amount`, `item_amount`, `item_status`, `sequential_rate`, `create_time`)
                        SELECT tmp,1697,t_item_id,t_item_name,1,+a,a*100,1,a+1,1606889089 FROM DUAL;
                END IF;                
                SET a= a+1;
        END WHILE;
                SET a=1;#重新赋值变量a,不然只会第二层只会循环一次
        SET m=m+1;    
        END WHILE;
    COMMIT;
END ;;
DELIMITER ;
CALL jyf_test();

 

mysql while嵌套

上一篇:MySQL:不可重复读演示


下一篇:sql执行计划-explain关键字