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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -