将一个表中的数据循环插入另外一个表中的mysql的存储过程

DELIMITER $$
CREATE PROCEDURE InsertDataIntoSysDictData()
BEGIN
    -- 声明变量
    DECLARE done INT DEFAULT FALSE;
    DECLARE attribute_name VARCHAR(255);
    DECLARE apply_mode VARCHAR(255);
		DECLARE counter1 INT DEFAULT 0;

    -- 游标声明
    DECLARE cur CURSOR FOR 
        SELECT t.attribute_name 
        FROM product_class_attribute_info t 
        WHERE t.query_flag = '1' 
        GROUP BY t.attribute_name;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    -- 打开游标
    OPEN cur;

    read_loop: LOOP
        FETCH cur INTO attribute_name;
        IF done THEN
            LEAVE read_loop;
        END IF;
				
				
        SET counter1 = counter1 + 1;
        -- 循环插入数据到 sys_dict_data 表
        INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value,dict_type,dict_attr,status,reg_user_code) VALUES (1,attribute_name,counter1, 'product_form_flag','01','0','qxq');
        SET counter1 = counter1 + 1;
				-- 循环插入数据到 sys_dict_data 表
        INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value,dict_type,dict_attr,status,reg_user_code) VALUES (2,attribute_name,counter1, 'product_form_flag','02','0','qxq');
        SET counter1 = counter1 + 1;
				-- 循环插入数据到 sys_dict_data 表
        INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value,dict_type,dict_attr,status,reg_user_code) VALUES (3,attribute_name,counter1, 'product_form_flag','03','0','qxq');
    END LOOP;

    -- 关闭游标
    CLOSE cur;
END$$

DELIMITER ;

  

posted on 2024-09-26 16:36  IT-QI  阅读(6)  评论(0编辑  收藏  举报