DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   MySQL (http://www.daniweb.com/forums/forum126.html)
-   -   select syntax for variable tablename?? (http://www.daniweb.com/forums/thread118774.html)

anso_pro Apr 12th, 2008 11:00 am
select syntax for variable tablename??
 
can anyone tell me how to use a variable table name in select syntax??
like..select * from tablename
here tablename is a variable name ..i read tht it can be done using dynamic sql..but i am not able to do it..can anyone tell me the code for it...i tried writin a procedure like this..

create procedure vartable(tablename varchar(255)
begin
declare varsql varchar(4000);
varsql='select * from tablename';
exec(varsql);
end;

is this right?? or am i doin any syntax or semantic error??

mwasif Apr 13th, 2008 1:53 pm
Re: select syntax for variable tablename??
 
SET @a = 'table';
SET @x := CONCAT('SELECT * FROM ', CONCAT(@a, '_', 'name'));
Prepare stmt FROM @x;
Execute stmt;
is same as
SELECT * FROM table_name;


All times are GMT -4. The time now is 10:54 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC