今天小编给大家分享一下mysql游标的作用是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

在mysql中,游标有指针的作用,主要用于对查询数据库所返回的记录结果集进行遍历,以便进行相应的操作。 游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。关系数据库管理系统实质是面向集合的,在MySQL中并没有一种描述表中单一记录的表达形式,除非使用WHERE子句来限制只有一条记录被选中;所以有时必须借助于游标来进行单条记录的数据处理。

本教程操作环境:windows7系统、mysql8版本、Dell G3电脑。

MySQL游标(Cursor)

游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。

游标充当指针的作用。

尽管游标能遍历结果中的所有行,但他一次只指向一行。

游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操作。

关系数据库管理系统实质是面向集合的,在 MySQL 中并没有一种描述表中单一记录的表达形式,除非使用 WHERE 子句来限制只有一条记录被选中。所以有时我们必须借助于游标来进行单条记录的数据处理。

一般通过游标定位到结果集的某一行进行数据修改。

游标的用法

1、声明一个游标: declare 游标名称 CURSOR for table;(这里的table可以是你查询出来的任意集合)

2、打开定义的游标:open 游标名称;

3、获得下一行数据:FETCH 游标名称 into testrangeid,versionid;

4、需要执行的语句(增删改查):这里视具体情况而定

5、释放游标:CLOSE 游标名称;

注:mysql存储过程每一句后面必须用;结尾,使用的临时字段需要在定义游标之前进行声明。

实例

-BEGIN--定义变量declaretestrangeidBIGINT;declareversionidBIGINT;declaredoneint;--创建游标,并存储数据declarecur_testCURSORforselectidastestrangeid,version_idasversionidfromtp_testrange;--游标中的内容执行完后将done设置为1DECLARECONTINUEHANDLERFORNOTFOUNDSETdone=1;--打开游标opencur_test;--执行循环posLoop:LOOP--判断是否结束循环IFdone=1THENLEAVEposLoop;ENDIF;--取游标中的值FETCHcur_testintotestrangeid,versionid;--执行更新操作updatetp_data_executesetversion_id=versionidwheretestrange_id=testrangeid;ENDLOOPposLoop;--释放游标CLOSEcur_test;END-

例子2:

我们现在要用存储过程做一个功能,统计iphone的总库存是多少,并把总数输出到控制台。

--在windows系统中写存储过程时,如果需要使用declare声明变量,需要添加这个关键字,否则会报错。delimiter//dropprocedureifexistsStatisticStore;CREATEPROCEDUREStatisticStore()BEGIN--创建接收游标数据的变量declarecint;declarenvarchar(20);--创建总数变量declaretotalintdefault0;--创建结束标志变量declaredoneintdefaultfalse;--创建游标declarecurcursorforselectname,countfromstorewherename='iphone';--指定游标循环结束时的返回值declarecontinueHANDLERfornotfoundsetdone=true;--设置初始值settotal=0;--打开游标opencur;--开始循环游标里的数据read_loop:loop--根据游标当前指向的一条数据fetchcurinton,c;--判断游标的循环是否结束ifdonethenleaveread_loop;--跳出游标循环endif;--获取一条数据时,将count值进行累加操作,这里可以做任意你想做的操作,settotal=total+c;--结束游标循环endloop;--关闭游标closecur;--输出结果selecttotal;END;--调用存储过程callStatisticStore();

fetch是获取游标当前指向的数据行,并将指针指向下一行,当游标已经指向最后一行时继续执行会造成游标溢出。
使用loop循环游标时,他本身是不会监控是否到最后一条数据了,像下面代码这种写法,就会造成死循环;

read_loop:loopfetchcurinton,c;settotal=total+c;endloop;

在MySql中,造成游标溢出时会引发mysql预定义的NOT FOUND错误,所以在上面使用下面的代码指定了当引发not found错误时定义一个continue 的事件,指定这个事件发生时修改done变量的值。

declarecontinueHANDLERfornotfoundsetdone=true;

所以在循环时加上了下面这句代码:

--判断游标的循环是否结束ifdonethenleaveread_loop;--跳出游标循环endif;

如果done的值是true,就结束循环。继续执行下面的代码

使用方式

游标有三种使用方式:

第一种就是上面的实现,使用loop循环;

第二种方式如下,使用while循环:

dropprocedureifexistsStatisticStore1;CREATEPROCEDUREStatisticStore1()BEGINdeclarecint;declarenvarchar(20);declaretotalintdefault0;declaredoneintdefaultfalse;declarecurcursorforselectname,countfromstorewherename='iphone';declarecontinueHANDLERfornotfoundsetdone=true;settotal=0;opencur;fetchcurinton,c;while(notdone)dosettotal=total+c;fetchcurinton,c;endwhile;closecur;selecttotal;END;callStatisticStore1();

第三种方式是使用repeat执行:

dropprocedureifexistsStatisticStore2;CREATEPROCEDUREStatisticStore2()BEGINdeclarecint;declarenvarchar(20);declaretotalintdefault0;declaredoneintdefaultfalse;declarecurcursorforselectname,countfromstorewherename='iphone';declarecontinueHANDLERfornotfoundsetdone=true;settotal=0;opencur;repeatfetchcurinton,c;ifnotdonethensettotal=total+c;endif;untildoneendrepeat;closecur;selecttotal;END;callStatisticStore2();

游标嵌套

在mysql中,每个begin end 块都是一个独立的scope区域,由于MySql中同一个error的事件只能定义一次,如果多定义的话在编译时会提示Duplicate handler declared in the same block。

dropprocedureifexistsStatisticStore3;CREATEPROCEDUREStatisticStore3()BEGINdeclare_nvarchar(20);declaredoneintdefaultfalse;declarecurcursorforselectnamefromstoregroupbyname;declarecontinueHANDLERfornotfoundsetdone=true;opencur;read_loop:loopfetchcurinto_n;ifdonethenleaveread_loop;endif;begindeclarecint;declarenvarchar(20);declaretotalintdefault0;declaredoneintdefaultfalse;declarecurcursorforselectname,countfromstorewherename='iphone';declarecontinueHANDLERfornotfoundsetdone=true;settotal=0;opencur;iphone_loop:loopfetchcurinton,c;ifdonethenleaveiphone_loop;endif;settotal=total+c;endloop;closecur;select_n,n,total;end;begindeclarecint;declarenvarchar(20);declaretotalintdefault0;declaredoneintdefaultfalse;declarecurcursorforselectname,countfromstorewherename='android';declarecontinueHANDLERfornotfoundsetdone=true;settotal=0;opencur;android_loop:loopfetchcurinton,c;ifdonethenleaveandroid_loop;endif;settotal=total+c;endloop;closecur;select_n,n,total;end;beginend;endloop;closecur;END;callStatisticStore3();

上面就是实现一个嵌套循环,当然这个例子比较牵强。凑合看看就行。

动态SQL

Mysql 支持动态SQL的功能

set@sqlStr='select*fromtablewherecondition1=?';prepares1for@sqlStr;--如果有多个参数用逗号分隔executes1using@condition1;--手工释放,或者是connection关闭时,server自动回收deallocateprepares1;

以上就是“mysql游标的作用是什么”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。