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);
相关推荐
海南java第二人8 小时前
Nebula Graph 实战:基于图数据库存储 CMDB 实体关系
数据库·图数据库·nebula
曹牧9 小时前
oracle:“not all variables bound”
数据库·oracle
数据库百宝箱9 小时前
Oracle RMAN Image Copy 本地恢复
数据库·oracle
zuYM4g7Dp10 小时前
NoSql数据库设计心得
数据库·nosql
bjzhang7511 小时前
CentOS下安装MySQL详解
linux·mysql·centos
睡不醒男孩03082312 小时前
第七篇:揭秘 PostgreSQL 数据库内核级管控:CLup 深度架构设计与高可用底座技术白皮书
数据库·postgresql·clup
cmes_love12 小时前
Level 2逐笔成交历史数据下载方法笔记
数据库·笔记·oracle
swordbob13 小时前
MySQL字符集陷阱:从Oracle迁移踩坑到utf8mb4强制规范
数据库·sql
牛油果子哥q13 小时前
【C++ STL string 】C++ STL string 终极精讲:底层原理、内存机制、全套API、深浅拷贝、易错坑点与工程实战规范
数据库·c++
十五年专注C++开发13 小时前
MySql中各种功能用sql语句实现总结
数据库·sql·mysql