MySQL Procedure IN variable don't work properly -
i'm learning functions, procedures , triggers , wanted easy procedure count rows in table parameters.
create procedure countrows(in v varchar(30)) select count(*) v;
can tell me why if do:
call countrows('sometable'); call countrows(sometable); //i tried both
it don't work
sorry newbie question.
you need dynamic sql.
solution returning count of table passed parameter sp
delimiter $$ create procedure `countrows`(in v varchar(30)) begin set @t1 =concat("select count(*) ",v); prepare stmt3 @t1; execute stmt3; deallocate prepare stmt3; end$$ delimiter ;
execution
call countrows('sometable');
update: solution returning "table x contain n row(s)" table passed parameter sp
delimiter $$ create procedure `countrowsex`(in v varchar(30)) begin -- set @t1 =concat("select count(*) ",v); set @t1 =concat('set @totalrows=(select count(*) ',v, ' );'); prepare stmt3 @t1; execute stmt3; deallocate prepare stmt3; select concat( 'table ', v, ' contains ', @totalrows, ' row', if(@totalrows>1, 's','')); end$$ delimiter ;
execution
call countrowsex('sometable');
Comments
Post a Comment