zoukankan      html  css  js  c++  java
  • mysql之游标


    本文内容:

    • 什么是游标
    • 创建游标
    • 使用游标

    首发日期:2018-04-18


    什么是游标:

    • 如果你前面看过mysql函数,会发现无法使用返回多行结果的语句。但如果你又确实想要使用时,就需要使用到游标,游标可以帮你选择出某个结果(这样就可以做到返回单个结果)。
    • 另外,使用游标也可以轻易的取出在检索出来的行中前进或后退一行或多行的结果。
    • 游标可以遍历返回的多行结果。

    补充:

    • Mysql中游标只适用于存储过程以及函数。


    创建游标:

    • 语法:
      • 1.定义游标:declare 游标名 cursor for select语句;
      • 2.打开游标:open 游标名;
      • 获取结果:fetch 游标名 into 变量名[,变量名];
      • 关闭游标:close 游标名;
      • create procedure p1()
        begin
            declare id int;
            declare name varchar(15);
            -- 声明游标
            declare mc cursor for select * from class;
            -- 打开游标
            open mc;
            -- 获取结果
            fetch mc into id,name;
            -- 这里是为了显示获取结果
            select id,name;
            -- 关闭游标
            close mc;
            
        end;
        create procedure p2()
        begin
            declare id int;
            declare name varchar(15);
            -- 声明游标
            declare mc cursor for select * from class;
            -- 打开游标
            open mc;
            -- 获取结果
            loop  -- 循环,将表的内容都转移到class2中
            fetch mc into id,name;
            -- 这里是为了显示获取结果
            insert into class2 values(id,name);
            -- 关闭游标
            end loop;
            close mc;
            
        end;


    使用游标:

    • 游标每一次fetch都是获取一行结果,可以使用变量来获取fetch到的每一列的值
      create procedure p2()
      begin
          declare id int;
          declare name varchar(15);
          -- 声明游标
          declare mc cursor for select * from class;
          -- 打开游标
          open mc;
          -- 获取结果
          loop  -- 循环,将表的内容都转移到class2中
          fetch mc into id,name;
          -- 这里是为了显示获取结果
          insert into class2 values(id,name);
          -- 关闭游标
          end loop;
          close mc;
          
      end;

    上面的代码会有一个报错image,不断循环的话,始终会达到表的末尾,到了末尾就无法继续fetch,一般来说都要避免报错,到了末尾前会有一个mysql定义的

    create procedure p3()
    begin
        declare id int;
        declare name varchar(15);
        declare flag int default 0;
        -- 声明游标
        declare mc cursor for select * from class;
        declare continue handler for not found set flag = 1;
        -- 打开游标
        open mc;
        -- 获取结果
        l2:loop 
        
        fetch mc into id,name;
        if flag=1 then -- 当无法fetch会触发handler continue
            leave l2;
        end if;
        -- 这里是为了显示获取结果
        insert into class2 values(id,name);
        -- 关闭游标
        end loop;
        close mc;
        
    end;
    
    call p3();-- 不报错
    select * from class2;


  • 相关阅读:
    CodeForces Round #545 Div.2
    HDU 2222 Keywords Search
    拓扑排序
    CodeForces Round #553 Div2
    CodeForces Round #552 Div.3
    CodeForces Round #549 Div.2
    #Leetcode# 997. Find the Town Judge
    Educational Codeforces Round 62
    #Leetcode# 524. Longest Word in Dictionary through Deleting
    圆方树小结
  • 原文地址:https://www.cnblogs.com/progor/p/8875100.html
Copyright © 2011-2022 走看看