沈阳企业网站开发定制,更换网站程序,公司有域名 如何做网站,苏州朗冠网站建设公司一、游标的基本概念游标#xff1a;游标是一个存储在Mysql服务器上的数据库查询#xff0c;它不是一条select语句#xff0c;而是被该语句检索出来的结果集。本人#xff0c;学习游标中#xff0c;曾遇到一个问题#xff0c;循环总是最后多执行一次。下面分析程序#x…一、游标的基本概念游标游标是一个存储在Mysql服务器上的数据库查询它不是一条select语句而是被该语句检索出来的结果集。本人学习游标中曾遇到一个问题循环总是最后多执行一次。下面分析程序这个是一个sql脚本程序#if d0 then #end if; 注释掉这两行时会发现游标中的repeat循环总是多执行一次。vendors 表中之前的数据为图1二、程序及结果分析delimiter //create procedure procursor(in num int)begindeclare d boolean default 0;declare o int;declare t int;declare c int default 0;declare mycur cursor for select vend_id from vendors;declare continue handler for sqlstate 02000 set d 1 ;create table if not exists results(re_id int,re_num int);open mycur;repeatfetch mycur into o;#if d0 thenselect o;# set to*num;insert into results values(o,num*o);set cc1;select d;# end if;until d end repeat;close mycur;select * from results;select c;end //delimiter ;注释 #if d0 then 和# end if; 执行以上sql语句后call procursor(100); 执行结果如图2所示发现游标的循环总是多执行了一次执行了4次,分析发现原因在于最后一次fetch mycur into o;时mycur 为空 o值未更改所以最后一组值多执行了一次。此时若检测d的值发现d为1。将#if d0 then和# end if;注释去掉做一个条件判断后的结果如图3所示。结果正确 图2 图3