delimiter ; drop table if exists `t1`; create table `t1`(`id` int primary key)engine=myisam; insert into `t1` values (1),(2),(3),(4),(5),(6); drop procedure if exists `p1`; delimiter // create procedure `p1`(`num` int unsigned) begin declare `i` int unsigned default '0'; set @`qry`='select * from `t1` where '; select concat('building the query with ',`num`,' predicates') as `status`; repeat if `i`<`num`-1 then set @`qry`=concat(@`qry`,' `id` <> ',`i`,' and '); else set @`qry`=concat(@`qry`,' `id` <> ', `i`,' '); end if; set `i`=`i`+1; until `i`>=`num` end repeat; #select @`qry`; select concat('executing the query with ',`num`,' predicates') as `status`; prepare `stmt` from @`qry`; execute `stmt`; deallocate prepare `stmt`; end // delimiter ; call `p1`(10000);