chapter12_数据库编程_2_游标

本文详细介绍了MySQL中游标的使用方法,包括声明、打开、读取数据和关闭四个基本操作。通过具体示例展示了如何利用游标进行多行数据处理,如声明游标、设置条件处理、循环读取及插入数据等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 处理多行数据的操作要使用游标cursor

  • 游标有四个基本操作:

    (1) 声明 DECLARE

    (2) 打开 OPEN

    (3) 读取数据 FETCH

    (4) 关闭 CLOSE

  • 声明DECLARE

    (1) 语法

      DECLARE cursor_name CURSOR FOR select_statement
    

    (2) The SELECT statement cannot have an INTO clause

    (3) Cursor declarations must appear before handler declarations and after variable and condition declarations

    (4) A stored program may contain multiple cursor declarations, but each cursor declared in a given block must have a unique name

  • 打开OPEN

      OPEN cursor_name
    
  • 读取数据FETCH

    (1) 语法

      FETCH [[NEXT] FROM] cursor_name INTO var_name [, var_name] ...
    

    (2) This statement fetches the next row for the SELECT statement associated with the specified cursor (which must be open), and advances the cursor pointer

    (3) If a row exists, the fetched columns are stored in the named variables. The number of columns retrieved by the SELECT statement must match the number of output variables specified in the FETCH statement.

    (4) If no more rows are available, a No Data condition occurs with SQLSTATE value '02000'. To detect this condition, you can set up a handler for it (or for a NOT FOUND condition)

    (5) Be aware that another operation, such as a SELECT or another FETCH, may also cause the handler to execute by raising the same condition. If it is necessary to distinguish which operation raised the condition, place the operation within its own BEGIN ... END block so that it can be associated with its own handler

  • 关闭CLOSE

    (1) 语法

       CLOSE cursor_name
    

    (2) If not closed explicitly, a cursor is closed at the end of the BEGIN ... END block in which it was declared

  • 示例

      CREATE PROCEDURE curdemo()
    
      BEGIN
          DECLARE done INT DEFAULT FALSE;
          DECLARE a CHAR(16);
          DECLARE b, c INT;
          
          DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
          DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
          DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
          OPEN cur1;
          OPEN cur2;
    
          read_loop: LOOP
              FETCH cur1 INTO a, b;
              FETCH cur2 INTO c;
    
              IF done THEN
                  LEAVE read_loop;
              END IF;
              
              IF b < c THEN
                  INSERT INTO test.t3 VALUES (a,b);
              ELSE
                  INSERT INTO test.t3 VALUES (a,c);
              END IF;
          END LOOP;
    
          CLOSE cur1;
          CLOSE cur2;
      END; 
    
  • MYSQL使用游标示例

    procedure_curdemo.sql

      USE temp;
    
      CREATE TABLE t1 (
          id int(11) DEFAULT NULL,
          data int(11) DEFAULT NULL
      );
    
      CREATE TABLE t2 (
          i int(11) DEFAULT NULL
      );
    
      CREATE TABLE t3 (
          i int(11) DEFAULT NULL,
          j int(11) DEFAULT NULL
      );
    
      DELIMITER //
    
      CREATE PROCEDURE curdemo()
    
      BEGIN
          DECLARE done INT DEFAULT FALSE;
          DECLARE a CHAR(16);
          DECLARE b, c INT;
          
          DECLARE cur1 CURSOR FOR SELECT id,data FROM temp.t1;
          DECLARE cur2 CURSOR FOR SELECT i FROM temp.t2;
          DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
          OPEN cur1;
          OPEN cur2;
    
          read_loop: LOOP
              FETCH cur1 INTO a, b;
              FETCH cur2 INTO c;
    
              IF done THEN
                  LEAVE read_loop;
              END IF;
              
              IF b < c THEN
                  INSERT INTO temp.t3 VALUES (a,b);
              ELSE
                  INSERT INTO temp.t3 VALUES (a,c);
              END IF;
          END LOOP;
    
          CLOSE cur1;
          CLOSE cur2;
      END;//
    
      DELIMITER ;
基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生和教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值