利用mysql存储过程循环插入新数据并更新

利用mysql存储过程循环插入新数据并更新,第1张

DROP PROCEDURE IF EXISTS excute_job_v340

create procedure excute_job_v340()

begin 

        declare rdevid int//声明参数

        declare rech_id int

        declare slot int

        declare new_rech_id int

        declare new_price DOUBLE//声明参数

        declare done INT DEFAULT FALSE////声明结束标识参数

    --  声明游标

        DECLARE rdevrech_id CURSOR FOR

                select r.id as rdevid,r.rechargeconfig_id as rech_id,r.slot_no as slot from b_device_tbl dev

                LEFT JOIN r_device_rechargeconfig_tbl r on dev.id= r.device_id

                告裤where dev.dev_typedef_id =7 and dev.masterid is not NULL  and r.rechargeconfig_type=4 and r.is_deleted=0 and                 r.slot_no is not  NULL

-- 将结束标志绑定到游标

        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE

-- 打开游标

        OPEN  rdevrech_id   

-- 遍历

        read_loop: LOOP

-- 取值

                FETCH  NEXT from rdevrech_id INTO rdevid,rech_id,slot

                IF done THEN

                        LEAVE read_loop

                END IF// 结束判断

             猛并   select price into new_price from c_device_rechargeconfig_item_tbl where rechargeconfig_id =rech_id limit 1

                INSERT INTO `c_device_rechargeconfig_tbl` (  `type`, `style_id`, `is_default`, `dev_type_code`, `is_deleted`,                 `create_time`, `slot_no`)

                VALUES ( '4', NULL, '0', '0', '0', '2019-08-19 15:59:24',slot )

                select max(id) into new_rech_id from c_device_rechargeconfig_tbl

                INSERT INTO  `c_device_rechargeconfig_item_tbl` (  `price`, `goods`, `description`, `is_deleted`, `create_time`,        袜知简                 `rechargeconfig_id` )

                        VALUES (  new_price,new_price, '0.00', '0', '2019-08-19 15:59:24', new_rech_id)

                update r_device_rechargeconfig_tbl set rechargeconfig_id=new_rech_id where id=rdevid

        END LOOP

        CLOSE rdevrech_id

end;

call excute_job_v340() //调用执行

已经谨旁测试,创建清腔并运行下面的答晌衫存储过程可以循环添加数据:

create procedure dowhile()

begin

declare i int default 0

start transaction

while i<50 do

insert into users(userId,userName,userPwd) values(null,concat('s00',i),123456)

set i=i+1

end while

commit

end

delimiter


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11976422.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存