delimiter // drop table if exists `t1`// create table `t1`(`a` int,unique(`a`),`b` varchar(30))engine=myisam// insert into `t1` (`a`,`b`) values (1,'1'),(2,'2'); drop procedure if exists `p1`// create procedure `p1`(num int) begin declare count1 int default '0'; declare vb varchar(30); declare last_row int; while(num>=1) do set num=num-1; begin declare cur1 cursor for select `a` from `t1`; declare continue handler for not found set last_row = 1; set last_row:=0; open cur1; rep1: repeat begin declare exit handler for 1062 begin end; fetch cur1 into vb; if (last_row = 1) then leave rep1; end if; end; until last_row=1 end repeat; close cur1; end; end while; end// delimiter ; call p1(20);