关于游标的一些总结

游标本质上是一种从select结果集中每次提取一条记录的机制,因此游标与select语句息息相关。
使用游标的步骤:
1.声明游标 declare 游标名 cursor for select语句
2.打开游标 open 游标名
3.从游标中提取数据 fetch 游标名 into 变量(需要配合循环使用)
4.关闭游标 close 游标名称

备注:
1.变量名的个数必须与声明游标时使用的select语句结果集中的字段个数保持一致。
2.fetch在执行过程中如果无法提取数据会产生ERROR 1329(0200):Nodata to FETCH,这样我们可以自定义1329错误来结束遍历。

做特别复杂的判断的时候可能会用到游标,但是游标同触发器一样对性能影响很大,所以尽量不要用游标和触发器。

这里用Navicat for MySQL工具写一个demo。

首先创建一个学生表

 1 mysql> create table student(s_name varchar(20) not null default '不详',sex varchar(4) not null default '不详',s_no int(5) auto_increment,age int(3) not null,height int(3) not null,primary key(s_no));
 2 Query OK, 0 rows affected
 3 
 4 mysql> insert into student (s_name,sex,age,height) values('小张','',21,176);
 5 Query OK, 1 row affected
 6 
 7 mysql> insert into student (s_name,sex,age,height) values('小李','',22,175);
 8 Query OK, 1 row affected
 9 
10 mysql> insert into student (s_name,sex,age,height) values('小明','',25,178);
11 Query OK, 1 row affected
12 
13 mysql> insert into student (s_name,sex,age,height) values('小红','',23,165);
14 Query OK, 1 row affected
15 
16 mysql> insert into student (s_name,sex,age,height) values('小丽','',19,160);
17 Query OK, 1 row affected
18 
19 mysql> select * from student;
20 +--------+-----+------+-----+--------+
21 | s_name | sex | s_no | age | height |
22 +--------+-----+------+-----+--------+
23 | 小张   ||    1 |  21 |    176 |
24 | 小李   ||    2 |  22 |    175 |
25 | 小明   ||    3 |  25 |    178 |
26 | 小红   ||    4 |  23 |    165 |
27 | 小丽   ||    5 |  19 |    160 |
28 +--------+-----+------+-----+--------+
29 5 rows in set

利用游标将年龄小于23,身高低于175的学生的身高改为180,其他学生的身高改为169

 1 delimiter $$
 2 drop procedure if exists pro_student;
 3 create procedure pro_student()
 4 begin
 5     declare tage int;
 6     declare theight int;
 7     declare tno int;
 8     declare state varchar(20);
 9     declare cur_student cursor for select s_no,age,height from student;
10     declare continue handler for 1329 set state='error';
11     open cur_student;
12     repeat
13     fetch cur_student into tno,tage,theight;
14     if tage<23 and theight<175
15     then 
16         update student set height=180 where s_no=tno;
17     else
18         update student set height=169 where s_no=tno;
19     end if;
20     until state='error'
21     end repeat;
22     close cur_student;
23 end $$
24 delimiter;

运行之后调用

call pro_student();

学生表student变成了

 1 mysql> select * from student;
 2 +--------+-----+------+-----+--------+
 3 | s_name | sex | s_no | age | height |
 4 +--------+-----+------+-----+--------+
 5 | 小张   ||    1 |  21 |    169 |
 6 | 小李   ||    2 |  22 |    169 |
 7 | 小明   ||    3 |  25 |    169 |
 8 | 小红   ||    4 |  23 |    169 |
 9 | 小丽   ||    5 |  19 |    180 |
10 +--------+-----+------+-----+--------+
11 5 rows in set

 

posted on 2017-04-14 19:39  wangtianze  阅读(676)  评论(0编辑  收藏  举报

导航