MYSQL的游标

游标( CURSOR )是用来存储查询结果集的数据类型 , 在存储过程和函数中可以使用游标对结果集进 行循环的处理。游标的使用包括游标的声明、 OPEN 、 FETCH 和 CLOSE ,

声明

DECLARE 游标名称 CURSOR FOR 查询语句 ;

打开游标

OPEN 游标名称 ;

获取游标记录

FETCH 游标名称 INTO 变量 [, 变量 ] ;

关闭游标

CLOSE 游标名称 ;

案例

根据传入的参数 uage ,来查询用户表 tb_user 中,所有的用户年龄小于等于 uage 的用户姓名
( name )和专业( profession ),并将用户的姓名和专业插入到所创建的一张新表 (id,name,profession)中

复制代码
create procedure p11(in uage int)
begin
	declare uname varchar(100);
	declare upro varchar(100);
	-- A. 声明游标, 存储查询结果集
	declare u_cursor cursor for select name,profession from tb_user where age <=uage;
drop table if exists tb_user_pro;
-- B. 准备: 创建表结构
create table if not exists tb_user_pro(
	id int primary key auto_increment,
	name varchar(100),
	profession varchar(100)
);
-- C. 开启游标
open u_cursor;
while true do
-- D. 获取游标中的记录
fetch u_cursor into uname,upro;
-- E. 插入数据到新表中
insert into tb_user_pro values (null, uname, upro);
end while;
-- F. 关闭游标
close u_cursor;
end;

call p11(30);

条件处理程序

上述的功能,虽然我们实现了,但是逻辑并不完善,而且程序执行完毕,获取不到数据,数据库还报错。 接下来,我们就需要来完成这个存储过程,并且解决这个问题。要想解决这个问题,就需要通过MySQL 中提供的 条件处理程序 Handler 来解决。

复制代码
DECLARE handler_action HANDLER FOR condition_value [, condition_value]
... statement ;
handler_action 的取值:
CONTINUE: 继续执行当前程序
EXIT: 终止执行当前程序
condition_value 的取值:
SQLSTATE sqlstate_value: 状态码,如 02000
SQLWARNING: 所有以01开头的SQLSTATE代码的简写
NOT FOUND: 所有以02开头的SQLSTATE代码的简写
SQLEXCEPTION: 所有没有被SQLWARNING 或 NOT FOUND捕获的SQLSTATE代码的简写

案例

通过SQLSTATE指定具体的状态码

复制代码
create procedure p11(in uage int)
begin
	declare uname varchar(100);
	declare upro varchar(100);
	-- A. 声明游标, 存储查询结果集
	declare u_cursor cursor for select name,profession from tb_user where age <=uage;
	-- 声明条件处理程序 : 当SQL语句执行抛出的状态码为02000时,将关闭游标u_cursor,并退出
	declare exit handler for SQLSTATE '02000' close u_cursor;
	drop table if exists tb_user_pro;
	-- B. 准备: 创建表结构
	create table if not exists tb_user_pro(
		id int primary key auto_increment,
		name varchar(100),
		profession varchar(100)
	);
	-- C. 开启游标
	open u_cursor;
	while true do
	-- D. 获取游标中的记录
	fetch u_cursor into uname,upro;
	-- E. 插入数据到新表中
	insert into tb_user_pro values (null, uname, upro);
	end while;
	-- F. 关闭游标
	close u_cursor;
end;

-- 调用
call p11(30);

通过 SQLSTATE 的代码简写方式 NOT FOUND 02 开头的状态码

复制代码
create procedure p12(in uage int)
begin
	declare uname varchar(100);
	declare upro varchar(100);
	-- A. 声明游标, 存储查询结果集
	declare u_cursor cursor for select name,profession from tb_user where age <=uage;
	-- 声明条件处理程序 : 当SQL语句执行抛出的状态码为02开头时,将关闭游标u_cursor,并退出
	declare exit handler for not found close u_cursor;
	drop table if exists tb_user_pro;
	-- B. 准备: 创建表结构
	create table if not exists tb_user_pro(
		id int primary key auto_increment,
		name varchar(100),
		profession varchar(100)
	);
	-- C. 开启游标
	open u_cursor;
	while true do
	-- D. 获取游标中的记录
	fetch u_cursor into uname,upro;
	-- E. 插入数据到新表中
	insert into tb_user_pro values (null, uname, upro);
	end while;
	-- F. 关闭游标
	close u_cursor;
end;

-- 调用
call p11(30);
相关推荐
小兜全糖(xdqt)23 分钟前
pyspark 从postgresql读取数据
数据库·postgresql
姓刘的哦2 小时前
Qt中的QWebEngineView
数据库·c++·qt
心随_风动3 小时前
Ubuntu 文件复制大师:精通cp命令完整指南
数据库·ubuntu·postgresql
不要再敲了3 小时前
JDBC从入门到面试:全面掌握Java数据库连接技术
java·数据库·面试
恣艺6 小时前
Redis列表(List):实现队列/栈的利器,底层原理与实战
数据库·redis·list
秋难降6 小时前
零基础学习SQL(十一):SQL 索引结构|从 B+Tree 到 Hash,面试常问的 “为啥选 B+Tree” 有答案了
数据库·后端·mysql
代码的余温7 小时前
Linux内核调优实战指南
linux·服务器·数据库
almighty277 小时前
C# DataGridView表头自定义设置全攻略
数据库·c#·winform·datagridview·自定义表头
ljh5746491197 小时前
mysql 必须在逗号分隔字符串和JSON字段之间二选一,怎么选
数据库·mysql·json
论迹7 小时前
【Redis】-- 持久化
数据库·redis·缓存