数据库中有一个表:如下

SELECT id, descs FROM table_learn WHERE id>0 and id<4;

结果如下:



下面是一个demo,可以直接按照这个结果循环操作:


delimiter //
drop procedure if exists sp_name //
create procedure sp_name()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE v_id int;
DECLARE v_descs varchar(100);


#declare cursor
DECLARE cur1 CURSOR FOR SELECT id, descs
FROM table_learn
WHERE id>0 and id<4;

#declare handle
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

drop table if exists tmp_table;
create temporary table tmp_table(id int, descs varchar(100));


#open cursor
OPEN cur1;

#starts the loop
the_loop: LOOP

#get the values of each column into our variables
FETCH cur1 INTO v_id,v_descs;
IF done THEN
LEAVE the_loop;
END IF;

#Do some post processing
IF v_id IS NOT NULL THEN
SET v_descs = "haha";
END IF;

#Insert it
INSERT INTO tmp_table (id, descs)
VALUES (v_id, v_descs);

END LOOP the_loop;

CLOSE cur1;
select * from tmp_table;
end;

//
delimiter ;
call sp_name();

更多相关文章

  1. MySQL基本操作汇总
  2. MySQL修改表结构操作命令总结
  3. mysql主从复制配置操作以及主主配置宕机切换演练
  4. MySQL在Linux下Shell脚本操作命令
  5. 不知道有人在mysql5.0上 针对10亿条数据操作过没有
  6. linux环境下写C++操作mysql(一)
  7. iframe操作、调用父页面元素或js函数
  8. 11、javascript中字符串常用操作总结、JS字符串操作大全
  9. javascript应该注意的小case--操作符

随机推荐

  1. 在单选按钮上选中/取消选中,加载/隐藏部分
  2. javascript 的MD5代码备份,跟java互通
  3. 使用AngularJS隐藏滚动div
  4. 简单实现一个文件上传的进度条
  5. 如何在当前视图中始终保持水平底部滚动条
  6. JavaScript(ES5)使用保留字作函数名
  7. JQuery纯前端导入Excel文件,兼容IE10及IE9
  8. 如何将图像(PNG)转换为2D数组(二进制图像)?
  9. 当鼠标悬停在顶部的对象上时,SVG悬停被取
  10. JavaScript动态显示时间