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);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值