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);
相关推荐
小吴编程之路7 小时前
MySQL 索引核心特性深度解析:从底层原理到实操应用
数据库·mysql
~莫子7 小时前
MySQL集群技术
数据库·mysql
凤山老林7 小时前
SpringBoot 使用 H2 文本数据库构建轻量级应用
java·数据库·spring boot·后端
就不掉头发7 小时前
Linux与数据库进阶
数据库
与衫7 小时前
Gudu SQL Omni 技术深度解析
数据库·sql
咖啡の猫8 小时前
Redis桌面客户端
数据库·redis·缓存
oradh8 小时前
Oracle 11g数据库软件和数据库静默安装
数据库·oracle
what丶k8 小时前
如何保证 Redis 与 MySQL 数据一致性?后端必备实践指南
数据库·redis·mysql
_半夏曲8 小时前
PostgreSQL 13、14、15 区别
数据库·postgresql
把你毕设抢过来8 小时前
基于Spring Boot的社区智慧养老监护管理平台(源码+文档)
数据库·spring boot·后端