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);
相关推荐
稚辉君.MCA_P8_Java5 小时前
JVM第二课:一文讲透运行时数据区
jvm·数据库·后端·容器
阳光明媚sunny6 小时前
Room持久化库中,@Transaction注解的正确使用场景是?
android·数据库
北极糊的狐6 小时前
MySQL常见报错分析及解决方案总结(15)---Can’t connect to MySQL server on ‘localhost‘ (10061)
数据库·mysql
濑户川6 小时前
Django5 与 Vue3 表单交互全解析:从基础到实战
数据库
weixin_438077496 小时前
langchain官网翻译:Build a Question/Answering system over SQL data
数据库·sql·langchain·agent·langgraph
陈一Tender7 小时前
JavaWeb后端实战(MySql基础)
mysql
-雷阵雨-7 小时前
MySQL——数据库操作攻略
数据库·mysql
krielwus7 小时前
Oracle ORA-01653 错误检查以及解决笔记
数据库·oracle
Wadli7 小时前
csdn| MySQL
数据库·mysql
程序员水自流8 小时前
MySQL InnoDB存储引擎关键核心特性详细介绍
java·数据库·mysql