• MYSQL的游标


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

    声明

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

    打开游标

    OPEN 游标名称 ;

    获取游标记录

    FETCH 游标名称 INTO 变量 [, 变量 ] ;

    关闭游标

    CLOSE 游标名称 ;

    案例

    根据传入的参数 uage ,来查询用户表 tb_user 中,所有的用户年龄小于等于 uage 的用户姓名
    name )和专业( profession ),并将用户的姓名和专业插入到所创建的一张新表 (id,name,profession)中
    1. create procedure p11(in uage int)
    2. begin
    3. declare uname varchar(100);
    4. declare upro varchar(100);
    5. -- A. 声明游标, 存储查询结果集
    6. declare u_cursor cursor for select name,profession from tb_user where age <=uage;
    7. drop table if exists tb_user_pro;
    8. -- B. 准备: 创建表结构
    9. create table if not exists tb_user_pro(
    10. id int primary key auto_increment,
    11. name varchar(100),
    12. profession varchar(100)
    13. );
    14. -- C. 开启游标
    15. open u_cursor;
    16. while true do
    17. -- D. 获取游标中的记录
    18. fetch u_cursor into uname,upro;
    19. -- E. 插入数据到新表中
    20. insert into tb_user_pro values (null, uname, upro);
    21. end while;
    22. -- F. 关闭游标
    23. close u_cursor;
    24. end;
    25. call p11(30);

    条件处理程序

    上述的功能,虽然我们实现了,但是逻辑并不完善,而且程序执行完毕,获取不到数据,数据库还报错。 接下来,我们就需要来完成这个存储过程,并且解决这个问题。要想解决这个问题,就需要通过MySQL 中提供的 条件处理程序 Handler 来解决。
    1. DECLARE handler_action HANDLER FOR condition_value [, condition_value]
    2. ... statement ;
    3. handler_action 的取值:
    4. CONTINUE: 继续执行当前程序
    5. EXIT: 终止执行当前程序
    6. condition_value 的取值:
    7. SQLSTATE sqlstate_value: 状态码,如 02000
    8. SQLWARNING: 所有以01开头的SQLSTATE代码的简写
    9. NOT FOUND: 所有以02开头的SQLSTATE代码的简写
    10. SQLEXCEPTION: 所有没有被SQLWARNINGNOT FOUND捕获的SQLSTATE代码的简写

     案例

    通过SQLSTATE指定具体的状态码

    1. create procedure p11(in uage int)
    2. begin
    3. declare uname varchar(100);
    4. declare upro varchar(100);
    5. -- A. 声明游标, 存储查询结果集
    6. declare u_cursor cursor for select name,profession from tb_user where age <=uage;
    7. -- 声明条件处理程序 : 当SQL语句执行抛出的状态码为02000时,将关闭游标u_cursor,并退出
    8. declare exit handler for SQLSTATE '02000' close u_cursor;
    9. drop table if exists tb_user_pro;
    10. -- B. 准备: 创建表结构
    11. create table if not exists tb_user_pro(
    12. id int primary key auto_increment,
    13. name varchar(100),
    14. profession varchar(100)
    15. );
    16. -- C. 开启游标
    17. open u_cursor;
    18. while true do
    19. -- D. 获取游标中的记录
    20. fetch u_cursor into uname,upro;
    21. -- E. 插入数据到新表中
    22. insert into tb_user_pro values (null, uname, upro);
    23. end while;
    24. -- F. 关闭游标
    25. close u_cursor;
    26. end;
    27. -- 调用
    28. call p11(30);
    通过 SQLSTATE 的代码简写方式 NOT FOUND 02 开头的状态码
    1. create procedure p12(in uage int)
    2. begin
    3. declare uname varchar(100);
    4. declare upro varchar(100);
    5. -- A. 声明游标, 存储查询结果集
    6. declare u_cursor cursor for select name,profession from tb_user where age <=uage;
    7. -- 声明条件处理程序 : 当SQL语句执行抛出的状态码为02开头时,将关闭游标u_cursor,并退出
    8. declare exit handler for not found close u_cursor;
    9. drop table if exists tb_user_pro;
    10. -- B. 准备: 创建表结构
    11. create table if not exists tb_user_pro(
    12. id int primary key auto_increment,
    13. name varchar(100),
    14. profession varchar(100)
    15. );
    16. -- C. 开启游标
    17. open u_cursor;
    18. while true do
    19. -- D. 获取游标中的记录
    20. fetch u_cursor into uname,upro;
    21. -- E. 插入数据到新表中
    22. insert into tb_user_pro values (null, uname, upro);
    23. end while;
    24. -- F. 关闭游标
    25. close u_cursor;
    26. end;
    27. -- 调用
    28. call p11(30);

  • 相关阅读:
    怎么选择好的游戏平台开发商?
    Python排序算法
    1.mysql字段类型该怎么选择?
    YOLOv5结合GradCAM热力图可视化
    【XML编程与WEB服务】XML入门
    分布式系列分布式计算框架Hadoop核心组件概述
    调研280+篇文献!CVPR最佳论文得主清华黄高团队提出首篇动态网络综述,全面回顾动态网络的发展!
    [短记] speechbrain
    Java内部类分类
    武汉流星汇聚:跨境电商蓝海市场,比利时在线市场增长迅速
  • 原文地址:https://blog.csdn.net/qq_63431773/article/details/132953884