fetchSeqLoop:Loop fetch cursor into _seqname, _value; end Loop;
现在是死循环,还没有退出的条件,那么在这里和oracle有区别,Oracle的PL/SQL的指针有个隐性变量%notfound,Mysql是通过一个Error handler的声明来进行判断的, declare continue handler for Not found (do some action); 在Mysql里当游标遍历溢出时,会出现一个预定义的NOT FOUND的Error,我们处理这个Error并定义一个continue的handler就可以叻,关于Mysql Error handler可以查询Mysql手册定义一个flag,在NOT FOUND,标示Flag,在Loop里以这个flag为结束循环的判断就可以叻。
declare fetchSeqOk boolean; ## define the flag for loop judgement declare _seqname varchar(50); ## define the varient for store the data declare _value bigint(20); declare fetchSeqCursor cursor for select seqname, value from sys_sequence;## define the cursor declare continue handler for NOT FOUND set fetchSeqOk = true; ## define the continue handler for not found flag set fetchSeqOk = false; open fetchSeqCursor; fetchSeqLoop:Loop if fetchSeqOk then leave fetchSeqLoop; else fetch cursor into _seqname, _value; select _seqname, _value; end if; end Loop; close fetchSeqCursor;
declare fetchSeqOk boolean; ## define the flag for loop judgement declare _seqname varchar(50); ## define the varient for store the data declare _value bigint(20); declare fetchSeqCursor cursor for select seqname, value from sys_sequence;## define the cursor declare continue handler for NOT FOUND set fetchSeqOk = true; ## define the continue handler for not found flag set fetchSeqOk = false; open fetchSeqCursor; fetchSeqLoop:Loop if fetchSeqOk then leave fetchSeqLoop; else fetch cursor into _seqname, _value; begin declare fetchSeqOk boolean default 'inner'; declare cursor2 cursor for select .... from ...;## define the cursor declare continue handler for NOT FOUND set fetchSeqOk = true; ## define the continue handler for n ot set fetchSeqOk = false; open cursor2; fetchloop2 loop if fetchSeqOk then else end if; end loop; close cursor2; end; end if; end Loop; close fetchSeqCursor;
付:Mysql也有类似Oracle里的execute immediate的动态SQL的功能,通过这个功能可有多少弥补一些动态游标的缺憾叻 set @sqlStr='select * from table where condition1 = ?'; prepare s1 for @sqlStr; execute s1 using @condition1; 如果有多个参数用逗号分隔 deallocate prepare s1; 手工释放,或者是connection关闭时,server自动回收。