志在指尖
用双手敲打未来

sql游标的使用方法(作用是什么)

sql游标的使用方法

游标实际上是一种能从包括多条数据记载的成果会集每次提取一条记载的机制。
游标充任指针的效果。
尽管游标能遍历成果中的一切行,但他一次只指向一行。
游标的效果便是用于对查询数据库所回来的记载进行遍历,以便进行相应的操作。
【用法】
一、声明一个游标:declare游标称号CURSORfortable;(这儿的table可以是你查询出来的恣意集合)
二、翻开界说的游标:open游标称号;
三、取得下一行数据:FETCH游标称号intotestrangeid,versionid;
四、需要履行的语句(增删改查):这儿视具体情况而定
五、开释游标:CLOSE游标称号;
注:mysql存储进程每一句后面必须用;结尾,运用的暂时字段需要在界说游标之前进行声明。sql
【实例】
-BEGIN–界说变量declaretestrangeidBIGINT;declareversionidBIGINT;declaredoneint;–创立游标,并存储数据declarecur_testCURSORforselectidastestrangeid,version_idasversionidfromtp_testrange;–游标中的内容履行完后将done设置为1DECLARECONTINUEHANDLERFORNOTFOUNDSETdone=1;–翻开游标opencur_test;–履行循环posLoop:LOOP–判别是否完毕循环IFdone=1THENLEAVEposLoop;ENDIF;–取游标中的值FETCH
例子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预界说的NOTFOUND错误,所以在上面运用下面的代码指定了当引发notfound错误时界说一个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中,每个beginend块都是一个独立的scope区域,由于MySql中同一个error的事情只能界说一次,假如多界说的话在编译时会提示Duplicatehandlerdeclaredinthesameblock。
仿制代码
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;

sql游标的作用是什么

游标在处理数据中提供了在结果集中一次一行或者多行前进或向后浏览数据的能力,可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许用户对指定位置的数据进行处理。
游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。游标可以被看作是一个查询结果集和结果集中指向特定记录的游标位置组成的一个临时文件,提供了在查询结果集中向前或向后浏览数据、处理结果集中数据的能力。
有了游标,用户就可以访问结果集中任意一行数据,在将游标放置到某行之后,可以在该行或从该位置的行块上执行操作。

未经允许不得转载:IT技术网站 » sql游标的使用方法(作用是什么)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载