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自動回收。