drop procedure if exists `test`.`p_test`; delimiter $$ create procedure `test`.`p_test` (in _param int) language sql modifies sql data sql security definer begin drop table if exists `test`.`test`; if _param = 1 then set @sql = ' create table `test`.`test` ( `a` char(1) not null ) type = myisam '; prepare query from @sql; execute query; insert into `test`.`test` (`a`) values ('a'),('b'); else set @sql = ' create table `test`.`test` ( `b` char(1) not null ) type = myisam '; prepare query from @sql; execute query; insert into `test`.`test` (`b`) values ('c'),('d'); end if; # here comes the error select * from `test`.`test`; # this would work #set @sql = 'select * from `test`.`test`'; #prepare query from @sql; #execute query; end; $$ delimiter ; call `test`.`p_test`(1); call `test`.`p_test`(2); call `test`.`p_test`(1); call `test`.`p_test`(2);