mysql存储过程从表取数据存储到游标然后批量修改
时间:2019-02-18 12:55 来源:linux.it.net.cn 作者:IT
delimiter $
create PROCEDURE category()
BEGIN
DECLARE categoryName varchar(50);
DECLARE parentId bigint(20);
DECLARE itemId bigint(20);
-- 遍历数据结束标志
DECLARE done INT DEFAULT FALSE;
-- 游标
DECLARE cur_account CURSOR FOR SELECT a.id,b.name,b.parent_id FROM tb_item a, tb_item_cat b where a.cid = b.id;
-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- 打开游标
OPEN cur_account;
-- 遍历
read_loop: LOOP
-- 取值 取多个字段
FETCH NEXT from cur_account INTO itemId,categoryName,parentId;
IF done THEN
LEAVE read_loop;
END IF;
-- 你自己想做的操作
update tb_item set name = categoryName,parent_id = parentId where id = itemId;
END LOOP;
CLOSE cur_account;
END $
(责任编辑:IT)
delimiter $
create PROCEDURE category()
BEGIN
DECLARE categoryName varchar(50);
DECLARE parentId bigint(20);
DECLARE itemId bigint(20);
-- 遍历数据结束标志
DECLARE done INT DEFAULT FALSE;
-- 游标
DECLARE cur_account CURSOR FOR SELECT a.id,b.name,b.parent_id FROM tb_item a, tb_item_cat b where a.cid = b.id;
-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- 打开游标
OPEN cur_account;
-- 遍历
read_loop: LOOP
-- 取值 取多个字段
FETCH NEXT from cur_account INTO itemId,categoryName,parentId;
IF done THEN
LEAVE read_loop;
END IF;
-- 你自己想做的操作
update tb_item set name = categoryName,parent_id = parentId where id = itemId;
END LOOP;
CLOSE cur_account;
END $
|