954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Forming table name dynamically

Hello everyone,

I have to query a table according to the current item being fetched in the select statement.

What I meant, I want to form the name of the table and query it on runtime. For example, there is an item that comes from items table and it has a title and there is a table has the name : units_{title} contains informations about these kind of items.

Is there any ideas to do that with mysql statement without the need to do PHP code?

Thanks

osm1987
Newbie Poster
1 post since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Perhaps you can do it with a stored procedure (using parameters).

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

I never found a way to do that. You can use a case statement which lists all the allowed table names and chooses the right one depending on a procedure input parameter, but I don't think that you can have truly dynamic table names in stored procedures or queries.

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

Actually selecting the table and field names should be handles from front end application and the same need to be passed into back end and the database procedure should process the input parameters to process the job.

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: