cursor游标(mysql)

/*
游标 cursor
什么是游标?为什么需要游标
使用存储过程对sql进行编程的时候,我们查询的语句可能是数据是多个,它总是一口气全部执行,我们无法针对每一条进行判断。也就是说,我们无法控制程序的运行,所以引入了游标cursor
cursor类似于java中的迭代器。  它利用查询语句生成一个游标,然后游标中有一个类似指针的东西。首先指在游标首,就是迭代器。不解释了

   cursor 游标
   declare声明;  declare 游标名 cursor for select_statement;
   open 打开; open游标名
   fetch 取值; fetch 游标名 into var1,var2[,...]   select语句中查出的项有多少,就需要使用多少变量接受
   close 关闭; close 游标名
*/

create table goods
(
 id int,
 name varchar(20),
 num int
);
insert into goods values (1,‘dog‘,20),(2,‘cat‘,30),(3,‘pig‘,25);
select * from goods;

-- 游标在存储过程中使用

drop procedure p1;
create procedure p1()
begin

     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量

     open gs;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     close gs;

end;

call p1();

create procedure p2()
begin

     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量

     open gs;
     fetch gs into row_id,row_name,row_num;
     fetch gs into row_id,row_name,row_num;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     close gs;

end;

call p2(); --报错,如果取出游标数据的个数超过游标中数据的个数,报错。类似于数组越界

drop procedure p3;
create procedure p3()
begin

     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量

     open gs;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     fetch gs into row_id,row_name,row_num;
     select row_id,row_name,row_num;
     close gs;

end;
call p3();

--学会使用循环控制试试
create procedure p4()
begin
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare count_r int;
     declare i int default 0;

     declare gs cursor for select id,name,num from goods;  -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面
     select count(*) into count_r from goods;

     open gs;
     repeat
          fetch gs into row_id,row_name,row_num;
          select row_id,row_name,row_num;
          set i := i+1;
     until i>=count_r end repeat;
     close gs;
end;

call p4();  

-- 用while循环试试
create procedure p5()
begin
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare count_r int;
     declare i int default 0;

     declare gs cursor for select id,name,num from goods;  -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面
     select count(*) into count_r from goods;

     open gs;
     while i<count_r do
          fetch gs into row_id,row_name,row_num;
          select row_id,row_name,row_num;
          set i := i+1;
    end while;
     close gs;
end;

call p5();  

-- 使用游标最主要的是可以针对每一次查出来的结果进行一些操作
drop procedure p6;
create procedure p6()
begin
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;
     declare count_r int;
     declare i int default 0;

     declare gs cursor for select id,name,num from goods;  -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面
     select count(*) into count_r from goods;

     open gs;
     while i<count_r do
          fetch gs into row_id,row_name,row_num;
          if row_num>25 then select concat(row_name,‘比较多‘);
          elseif row_num=25 then select concat(row_name,‘刚刚好‘);
          else select concat(row_name,‘有点少‘);
          end if;
          set i := i+1;
    end while;
     close gs;
end;

call p6();

-- 第三种方式:游标越界时候使用标志,利用标识来结束
-- 在mysql cursor中,可以使用declare continue handler来操作一个越界标识
-- declare continue handler for not found statement;
drop procedure p7;
create procedure p7()
begin
     declare row_id int;
     declare row_name varchar(20);
     declare row_num int;

     declare you int default 1;
     declare gs cursor for select id,name,num from goods;
     declare continue handler for not found set you:=0;

     open gs;
      while you!=0 do
           fetch gs into row_id,row_name,row_num;
           if you!=0 then select row_num,row_name;
           end if;
     end while;
     close gs;
end;
call p7();
时间: 2024-08-02 23:59:50

cursor游标(mysql)的相关文章

【PLSQL】变量声明,结构语句,cursor游标

************************************************************************   ****原文:blog.csdn.net/clark_xu 徐长亮的专栏 ************************************************************************ PLSQL是ORACLE在标准SQL基础上增加了过程化处理,把DML和SELECT语句组织在PLSQL代码的过程性单元中. PLS

转 oracle cursor 游标

转自:http://blog.csdn.net/liyong199012/article/details/8948952 游标的概念:     游标是SQL的一个内存工作区,由系统或用户以变量的形式定义.游标的作用就是用于临时存储从数据库中提取的数据块.在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理,最后将处理结果显示出来或最终写回数据库.这样数据处理的速度才会提高,否则频繁的磁盘数据交换会降低效率. 游标有两种类型:显式游标和隐式游标.在前述程序中用到的SELECT...I

DRF url控制 解析器 响应器 版本控制 分页(常规分页,偏移分页,cursor游标分页)

url控制 第二种写法(只要继承了ViewSetMixin) url(r'^pub/$',views.Pub.as_view({'get':'list','post':'create'})), #获取所有记得路由后面加$结束符 #pub/?format=json url(r'^pub\.(?P<format>\w+)$',views.Pub.as_view({'get':'list','post':'create'})), #pub.json url(r'^pub/(?P<pk>\

SQL Cursor 游标的使用

Contents SQL Cursor 游标的使用 这两天在做新老系统间的data migration,接触到sql的游标,记录总结一下. 我们的需求是要求map多张表,并把计算结果分别更新到一张目标表中, 新旧系统要做A/B Testing, 所以当旧表有任何更新,比如新增,删除,改动, 都要更新到新表中. 原本我选择的方案是采用批量Insert, 但碰到一个需要插入map关系的表, 其中一个field是另外一张表刚刚插入数据的id, 因此只能用循环来解决. 看了一圈SQL的for循环,实现起

mysql cursor 游标

以下说明基于mysql 5.5. 概述:我知道大部分人对于mysql游标使用的不多.mysql大多数情况可以用“集合”操作,即可满足90%的需求.mysql cursor作为对“记录”操作,是操作数据的一种补充. mysql cursor三大特性(大三“坑”): 1.只读的:cursor本身不提供修改数据的操作,只能fetch columns into variables.(当然你可以把数据拿出来以后,再用update语句操作一下,但是有坑,第三点说明). 2.不能滚动的:只能向一个方向遍历数据

cursor(游标)

原理:游标就是把数据按照指定要求提取出相应的数据集,然后逐条进行数据处理. 游标的概念 1.1游标(Cursor) 它使用户可逐行访问由SQL Server返回的结果集. 使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式. 用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合. 游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录. 1.2SQL语言与主语言具有不同数据处理方式 (

SQL Cursor(游标)

1.游标在数据表没有id(identity(1,1))时好用,但是游标会吃更多的内存,减少可用的并发,占用宽带,锁定资源,当然还有更多的代码量 2.如果能不用游标,尽量不要使用游标,用完用完之后一定要关闭和释放, 尽量不要在大量数据上定义游标,尽量不要使用游标上更新数据 Cursor:Global for--全局游标 Cursor:Local for--局部游标 LOCAL意味着游标的生存周期只在批处理或函数或存储过程中可见 GLOBAL意味着游标对于特定连接作为上下文,全局内有效 --第一步:

SQL SERVER CURSOR游标的使用(转载)

一:认识游标 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集. 使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式. 用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合. 游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录. 二:游标的基本形式 声明游标:形式1DECLARE cursor_name [INSENSITIVE] [SCROLL] CURS

存储过程/游标/mysql 函数

存储过程和函数(存储在 mysql数据库中的 proc表,所以检查有没有这个表)存储过程是一种存储程序(如正规语言里的子程序一样),mysql支持有两种:存储过程,在其他SQL语句中可以返回值的函数(使用起来和 mysql预装载的函数一样,如 pi())一个存储过程包括名字,参数列表,以及可以包括很多SQL语句的SQL语句集.as:(复合语句块)CREATE PROCEDURE producedure1  /*name存储过程名*/(in parameter1 interger)       /