MySQL进阶之游标,条件处理程序及存储函数

游标

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

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

打开游标
OPEN 游标名称 ;

获取游标记录
FETCH 游标名称 INTO 变量 [, 变量 ] ;

关闭游标
CLOSE 游标名称 ;

案例:

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

create procedure p10(in uage int)
    begin
        declare uname varchar(100);
        declare upro varchar(100);
        declare u_cursor cursor for select name,profession from tb_user where age <= uage;

        create table if not exists tb_user_pro(
            id int primary key auto_increment,
            name varchar(100),
            profession varchar(100)

        );
        open u_cursor; -- 开启游标
        while true do
            fetch u_cursor into uname,upro;
            insert into tb_user_pro values (null,uname,upro);
            end while;
        close u_cursor;
end;
    end;

call p10(30);

 

上述的存储过程,最终我们在调用的过程中,会报错,之所以报错是因为上面的while循环中,并没有 退出条件。当游标的数据集获取完毕之后,再次获取数据,就会报错,从而终止了程序的执行。

条件处理程序

条件处理程序(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代码的简写

 A. 通过SQLSTATE指定具体的状态码

create procedure p10(in uage int)
    begin
        declare uname varchar(100);
        declare upro varchar(100);
        declare u_cursor cursor for select name,profession from tb_user where age <= uage;
        declare exit handler for sqlstate '02000' close u_cursor;

        create table if not exists tb_user_pro(
            id int primary key auto_increment,
            name varchar(100),
            profession varchar(100)

        );
        open u_cursor; -- 开启游标
        while true do
            fetch u_cursor into uname,upro;
            insert into tb_user_pro values (null,uname,upro);
            end while;
        close u_cursor;
end;
    end;

call p10(30);

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

create procedure p12(in uage int)
begin
	declare uname varchar(100);
	declare upro varchar(100);
	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;
	create table if not exists tb_user_pro(
	id int primary key auto_increment,
	name varchar(100),
	profession varchar(100)
	);
	open u_cursor;
	while true do
fetch u_cursor into uname,upro;
insert into tb_user_pro values (null, uname, upro);

	end while;
	close u_cursor;
end;
call p12(30);

 

存储函数

存储函数是有返回值的存储过程,存储函数的参数只能是IN类型的。

CREATE FUNCTION 存储函数名称 ([ 参数列表 ])
RETURNS type [characteristic ...]
BEGIN
-- SQL语句
RETURN ...;
END ;

 

characteristic说明:

  • DETERMINISTIC:相同的输入参数总是产生相同的结果

  • NO SQL :不包含 SQL 语句。

  • READS SQL DATA:包含读取数据的语句,但不包含写入数据的语句。

计算从1累加到n的值,n为传入的参数值。
create function fun1(n int)
returns int deterministic
begin
    declare total int default 0;
    while n > 0 do
        set total := total + n;
        set n := n - 1;

        end while;
    return total;
end;
select fun1(50);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

元气满满的热码式

感谢您的支持!我会继续努力发布

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值