0

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??

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by mwasif
0
SET @a = 'table';
SET @x := CONCAT('SELECT * FROM ', CONCAT(@a, '_', 'name'));
Prepare stmt FROM @x;
Execute stmt;

is same as

SELECT * FROM table_name;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.