Home > Database > Mysql Tutorial > MySql游标的使用实例_MySQL

MySql游标的使用实例_MySQL

WBOY
Release: 2016-06-01 13:08:36
Original
1199 people have browsed it

mysql游标使用的整个过程为:

1.创建游标
复制代码 代码如下:DECLARE calc_bonus CURSOR FOR SELECT id, salary, commission FROM employees;

2.打开游标
复制代码 代码如下:OPEN calc_bonus;

3.使用游标
复制代码 代码如下:FETCH calc_bonus INTO re_id, re_salary, re_comm;

4.关闭游标
复制代码 代码如下:CLOSE calc_bonus;

实例代码如下所示:

复制代码 代码如下:
begin
declare temp_user_id int default null;
declare stop int default 0;
#声明游标
 declare temp_cur cursor for select f_user_id from table_test where f_user_id=1;
 #声明游标的异常处理
 declare continue handler for sqlstate '02000' set stop=1;
 open temp_cur;
 fetch temp_cur into temp_user_id;
 #判断游标是否到达最后
 while stop1 do
 #各种判断
 #读取下一行的数据   
 fetch temp_cur into temp_user_id;  
 #循环结束  
 end while; 
 #关闭游标
 close temp_cur;
end

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template