当前位置: 首页 > news >正文

mysql数据库存储过程之游标(光标cursor)

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

一、语法。

#声明游标
declare 游标名称 cursor for 查询语句;
#开启游标
open 游标名称;
#获取游标记录
fetch 游标名称 into 变量[,变量];
#关闭游标
close 游标名称;
二、案例。

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

逻辑

#A.声明游标,存储查询结果集

#B.创建表结构

#C.开启游标

#D.获取游标记录

#E.插入数据到新表中

#F.关闭游标

#创建一个存储过程
create procedure p11(in uage int)
begin
  declare uname varchar(100);#声明变量
  declary upro varchar(100);#声明变量
#声明游标记录符合条件的结果集
  declare u_cursor cursor for select name,profession from tb_user where age <= uage;
  drop table if exists tb_user_pro;  #tb_user_pro表如果存在,就删除。
  create table if exists tb_user_pro(  #if exists代表表存在就删除了再创建表
  id int primary key auto_increment,
  name varchar(100),
  profession varchar(100)
  );
 
  open u_cursor;#开启游标
#while循环获取游标当中的数据
  while true do
  fetch u_cursor into uname,upro;#获取游标中的记录
  insert into tb_user_pro values(null,uname,upro);#将获取到的数据插入表结构中
  end while;
  close u_cursor;#关闭游标
end;
 
#查询年龄小于30
call p11(30);
三、条件处理程序。

条件处理程序handler可以用来定义在流程控制结构执行过程中遇到问题时相应的处理步骤。

1、语法。

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代码的简写
2、解决报错。

#创建一个存储过程
create procedure p11(in uage int)
begin
  declare uname varchar(100);#声明变量
  declary upro varchar(100);#声明变量
#声明游标记录符合条件的结果集
  declare u_cursor cursor for select name,profession from tb_user where age <= uage;
#声明一个条件处理程序,当满足SQL状态码为02000的时候,触发退出操作,退出的时候将游标关闭
  declare exit handler for SQLSTATE '02000' close u_cursorl;
 
#声明一个条件处理程序,当满足SQL状态码为02000的时候,触发退出操作,退出的时候将游标关闭
  declare exit handler for not found close u_cursorl;
 
drop table if exists tb_user_pro;  #tb_user_pro表如果存在,就删除。
  create table if exists tb_user_pro(  #if exists代表表存在就删除了再创建表
  id int primary key auto_increment,
  name varchar(100),
  profession varchar(100)
  );
 
  open u_cursor;#开启游标
#while循环获取游标当中的数据
  while true do
  fetch u_cursor into uname,upro;#获取游标中的记录
  insert into tb_user_pro values(null,uname,upro);#将获取到的数据插入表结构中
  end while;
  close u_cursor;#关闭游标
end;
 
#查询年龄小于30
call p11(30);
 

http://www.lryc.cn/news/226992.html

相关文章:

  • 「帝国风暴兵」加入 The Sandbox,推出真实的全新人物化身系列和体验!
  • asp.net员工管理系统VS开发sqlserver数据库web结构c#编程包括出差、请假、考勤
  • C++套接字库sockpp介绍
  • Mac M2开发环境安装
  • Linux各种版本安装详细步骤和root密码破解
  • Netty - 回顾Netty高性能原理和框架架构解析
  • uni-app——項目day01
  • 【Java、MongoDB】程序控制非关系数据库
  • MySQL查询时间处理相关函数与方法实践笔记
  • springboot全局拦截sql异常
  • AlGaN/GaN HFET 五参数模型
  • 矩阵的除法
  • Java中的 向上转型 | 向下转型
  • 【华为OD机试AB高分必刷题目】朋友圈(C++-并查集Union-Find实现)
  • 前端面试题之vue篇
  • Java进阶(垃圾回收GC)——理论篇:JVM内存模型 垃圾回收定位清除算法 JVM中的垃圾回收器
  • GaN HEMT 电容的分析建模,包括寄生元件
  • Python实战 | 使用 Python 和 TensorFlow 构建卷积神经网络(CNN)进行人脸识别
  • JLink edu mini 10Pin接口定义
  • compile: version “go1.19“ does not match go tool version “go1.18.1“
  • spring boot security 自定义AuthenticationProvider
  • 某电力设计公司绩效考核优化项目成功案例纪实
  • 力扣371周赛
  • Python之字符串、正则表达式练习
  • Transmit :macOS 好用的 Ftp/SFtp 工具
  • 【Github】git clone命令下载文件中途停止
  • Clickhouse学习笔记(10)—— 查询优化
  • [量化投资-学习笔记012]Python+TDengine从零开始搭建量化分析平台-策略回测
  • MySQL 查看 event 执行记录
  • 开发知识点-Vue-Electron