cursor游标(mysql)
时间:2021-07-01 10:21:17
帮助过:5人阅读
游标 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();
cursor游标(mysql)
标签: