/*游标 cursor
什么是游标?为什么需要游标
使用存储过程对sql进行编程的时候,我们查询的语句可能是数据是多个,它总是一口气全部执行,我们无法针对每一条进行判断。也就是说,我们无法控制程序的运行,所以引入了游标cursor
cursor类似于java中的迭代器。 它利用查询语句生成一个游标,然后游标中有一个类似指针的东西。首先指在游标首,就是迭代器。不解释了
cursor 游标
declare声明; declare 游标名 cursor for select_statement;
open 打开; open游标名
fetch 取值; fetch 游标名 into var1,var2[,...] select语句中查出的项有多少,就需要使用多少变量接受
close 关闭; close 游标名*/
create tablegoods
(
idint,
namevarchar(20),
numint);insert into goods values (1,'dog',20),(2,'cat',30),(3,'pig',25);select * fromgoods;--游标在存储过程中使用
drop procedurep1;create procedurep1()begin
declare row_id int;declare row_name varchar(20);declare row_num int;declare gs cursor for select id,name,num from goods; --声明游标的语句后面不能有声明变量
opengs;fetch gs intorow_id,row_name,row_num;selectrow_id,row_name,row_num;closegs;end;
call p1();create procedurep2()begin
declare row_id int;declare row_name varchar(20);declare row_num int;declare gs cursor for select id,name,num from goods; --声明游标的语句后面不能有声明变量
opengs;fetch gs intorow_id,row_name,row_num;fetch gs intorow_id,row_name,row_num;fetch gs intorow_id,row_name,row_num;selectrow_id,row_name,row_num;closegs;end;
call p2();--报错,如果取出游标数据的个数超过游标中数据的个数,报错。类似于数组越界
drop procedurep3;create procedurep3()begin
declare row_id int;declare row_name varchar(20);declare row_num int;declare gs cursor for select id,name,num from goods; --声明游标的语句后面不能有声明变量
opengs;fetch gs intorow_id,row_name,row_num;selectrow_id,row_name,row_num;fetch gs intorow_id,row_name,row_num;selectrow_id,row_name,row_num;fetch gs intorow_id,row_name,row_num;selectrow_id,row_name,row_num;closegs;end;
call p3();--学会使用循环控制试试
create procedurep4()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 fromgoods;opengs;
repeatfetch gs intorow_id,row_name,row_num;selectrow_id,row_name,row_num;set i := i+1;
until i>=count_r endrepeat;closegs;end;
call p4();--用while循环试试
create procedurep5()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 fromgoods;opengs;while i
call p5();--使用游标最主要的是可以针对每一次查出来的结果进行一些操作
drop procedurep6;create procedurep6()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 fromgoods;opengs;while i25 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;closegs;end;
call p6();--第三种方式:游标越界时候使用标志,利用标识来结束--在mysql cursor中,可以使用declare continue handler来操作一个越界标识--declare continue handler for not found statement;
drop procedurep7;create procedurep7()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 fromgoods;declare continue handler for not found set you:=0;opengs;while you!=0dofetch gs intorow_id,row_name,row_num;if you!=0 then selectrow_num,row_name;end if;end while;closegs;end;
call p7();