MySQL游标
2019-01-23 11:54
876 查看
MySQL游标
While循环
-- MySQL游标只能用于存储过程和函数-- 使用过程 声明、打开游标、使用游标、关闭游标-- 如果未明确关闭游标,END时会自动关闭-- https://dev.mysql.com/doc/refman/8.0/en/error-handling.htmldrop PROCEDURE if exists process_user;create procedure process_user()begin -- Declare local variables declare done boolean default true; declare user_id int; declare user_info varchar(50); -- Declare the cursor declare cursor_user cursor for select id, user_name from sys_user; -- Declare continue handler declare continue handler for sqlstate '02000' set done=false; -- Create a table to store the results; create table if not exists process_user_result(user_id int, user_name varchar(50)); -- Open the cursor open cursor_user; -- Repeat through all rows repeat fetch cursor_user into user_id, user_info; if done then insert into process_user_result(user_id, user_name) values(user_id, user_info); end if; until (not done) end repeat; -- Close the cursor close cursor_user;end;call process_user();
Repeat循环
drop PROCEDURE if exists process_user;create procedure process_user()begin -- Declare local variables declare done boolean default true; declare user_id int; declare user_info varchar(50); -- Declare the cursor declare cursor_user cursor for select id, user_name from sys_user; -- Declare continue handler declare continue handler for not found set done=false; -- Create a table to store the results; create table if not exists process_user_result(user_id int, user_name varchar(50)); -- Open the cursor open cursor_user; -- Loop through all rows fetch cursor_user into user_id, user_info; while done do insert into process_user_result(user_id, user_name) values(user_id, user_info); fetch cursor_user into user_id, user_info; end while; -- Close the cursor close cursor_user;end;call process_user();
Loop循环
drop PROCEDURE if exists process_user;create procedure process_user()begin -- Declare local variables declare done boolean default true; declare user_id int; declare user_info varchar(50); -- Declare the cursor declare cursor_user cursor for select id, user_name from sys_user; -- Declare continue handler declare continue handler for not found set done=false; -- Create a table to store the results; create table if not exists process_user_result(user_id int, user_name varchar(50)); -- Open the cursor open cursor_user; -- Loop through all rows read_loop:loop fetch cursor_user into user_id, user_info; if not done then leave read_loop; end if; insert into process_user_result(user_id, user_name) values(user_id, user_info); end loop; -- Close the cursor close cursor_user;end;call process_user();
相关文章推荐
- MySql基础教程二(视图,存储过程,游标,触发器,事务,权限,数据类型)
- mysql 游标
- mysql存储过程 游标 循环使用 含SQLSTATE mysql异常代码汇总大全
- mysql存储过程游标嵌套示例
- mysql 游标的使用
- Mysql之WHILE替代游标实现子母表三层遍历拷贝
- MySQL-5.7 游标及DECLARE
- 创建角色随机名字(mysql抽取随机记录)和mysql游标的使用
- Mysql存储过程游标加循环
- mysql中游标的使用案例详解(学习笔记)
- MySQL游标
- mysql存储过程 游标 循环使用介绍
- mysql存储过程 游标 循环使用介绍
- 手把手教你mysql(十五)游标变量流程控制
- mysql语句:索引,游标,存储过程,视图,分区,分库分表,数据库集群,数据库负载均衡
- Mysql 游标嵌套
- mysql存储过程和游标遍历
- Mysql存储过程优化——使用临时表代替游标(转)
- mysql游标实例演示