美文网首页
chapter12_数据库编程_2_游标

chapter12_数据库编程_2_游标

作者: 米都都 | 来源:发表于2019-01-03 17:10 被阅读0次
  • 处理多行数据的操作要使用游标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 ;

相关文章

  • chapter12_数据库编程_2_游标

    处理多行数据的操作要使用游标cursor 游标有四个基本操作:(1) 声明 DECLARE(2) 打开 OPEN(...

  • sql 游标,函数,触发器

    游标是数据库的一种机制或类型,可以存储结果集 ,迭代和遍历结果集,oracle中游标大致分为显示游标和隐式游标。 ...

  • MySQL-与Python交互

    pymysql的使用流程 建立数据库连接 创建游标对象 使用游标对象的方法和SQL语句操控MySQL数据库 提交数...

  • chapter12_数据库编程_1_综述

    标准SQL语言是一种非过程性的数据库操作语言,不能实现控制流程、函数、子程序。因此,各个数据库软件都进行了扩展 过...

  • Python模块·PyMySQL数据库

    一、PyMySQL实现思路流程 1.建立连接2.创建游标:游标数据库操作的接口3.数据库操作(建表、插入数据、查询...

  • 好程序员Java培训教程分享oracle中的游标类型

    好程序员Java培训教程分享oracle中的游标类型,数据库中的游标(以下内容以Oracle为例): 游标是sql...

  • SQL第21课:使用游标

    mysql 游标的使用 游标是什么?? 游标是一个存储在MySQL服务器上的数据库查询,它不是一条select语句...

  • chapter12_数据库编程_3_存储过程

    存储过程(1) 存储过程是一个程序代码,存储在数据库中(2) 作用1° 接受输入参数,并以输出参数的格式向调用过程...

  • mysql 存储过程之游标

    游标按我的理解就是用在sql编程中对查询结果集的解析,类比jdbc中的resultset对象。FETCH 一行游标...

  • 数据库游标

    相关知识:https://www.cnblogs.com/taoshao/p/5448066.html

网友评论

      本文标题:chapter12_数据库编程_2_游标

      本文链接:https://www.haomeiwen.com/subject/bahqrqtx.html