473,752 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQL dynamic table name within query

Is it possible to have a dynamic table name within a query or a table
name that is a variable? This does not work but gives an example:

SELECT * FROM concat('table', 'name')
- OR -

SET @table = 'a';
SELCT * FROM @table

Of course the query can be constructed dynamically but does not exactly
fit the needs. The situation is very complicated and would just be
confusing to discuss here so please do not offer suggestions on how to
construct the query programically as that will not work for this
situation.

Thanks

May 16 '06 #1
3 52344
<ry******@yahoo .com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .
Is it possible to have a dynamic table name within a query or a table
name that is a variable?
No!
This does not work but gives an example:

SELECT * FROM concat('table', 'name')
- OR -

SET @table = 'a';
SELCT * FROM @table

Of course the query can be constructed dynamically but does not exactly
fit the needs. The situation is very complicated and would just be
confusing to discuss here so please do not offer suggestions on how to
construct the query programically as that will not work for this
situation.

Thanks

May 16 '06 #2
ry******@yahoo. com wrote:
Of course the query can be constructed dynamically but does not exactly
fit the needs. The situation is very complicated and would just be
confusing to discuss here so please do not offer suggestions on how to
construct the query programically as that will not work for this
situation.


Table names, column names, etc. cannot be dynamic in the way you
describe. This is not permitted by the SQL language, for many reasons.

For instance, there would be no way for the query optimizer to decide
which index(es) to use, if it doesn't know at parse time which tables
and columns are being queried.

Find another way to solve your problem.

Regards,
Bill K.
May 16 '06 #3
onetwitch
1 New Member
Actually, it is possible. What you need to do is first create a variable that will contain the name of your table, then put that into the sql statement, except don't put the variable within single quotes:

$dynamic_table_ name = "table"; (of course, this is where you would make it dynamic)

then, unlike the usual method of putting varibles in mysql statements - '$dynamic_table _name' - leave off the single quotes (') and just put
$dynamic_table_ name, i.e. "select * from $dynamic_table_ name......




Is it possible to have a dynamic table name within a query or a table
name that is a variable? This does not work but gives an example:

SELECT * FROM concat('table', 'name')
- OR -

SET @table = 'a';
SELCT * FROM @table

Of course the query can be constructed dynamically but does not exactly
fit the needs. The situation is very complicated and would just be
confusing to discuss here so please do not offer suggestions on how to
construct the query programically as that will not work for this
situation.

Thanks
Jun 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
6460
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the results below, I would think that the delete should have deleted row 1 {1 5 me) and not row 3 (1 5 they) when I run this statement delete t1 from t, t1 where t.id = t1.id and t.id=1 and t.name = 'me'; Any ideas on why row 2 is deleted?
2
29760
by: Martin Feuersteiner | last post by:
Hi I'm grateful for any light you can shed on this!! I've to admit, it's an unusual design but I've multiple contact tables named e.g. i2b_ash_contact or i2b_ted_contact. 'i2b_' and '_contact' are static but the middle part is dynamic. Storing all contacts in one table with an identifier of e.g. 'ash' or 'ted' for each record is not possible.
5
3941
by: phillip.s.powell | last post by:
$sql = "SELECT IF((SHOW TABLES LIKE '$subselectTableName'), count(*), NULL) AS numRows FROM $subselectTableName"; I am trying to write a SQL statement that will tell me if a table exists or not, this is because PHP will throw Fatal Errors and halt all programming if the table does not exist that I attempt to query. How do you write an elegant MySQL 4.1+ statement to detect if a table exists or not?
2
3310
by: larry777 | last post by:
Greetings, I'm a Php newbie and am developing a photo album site and have the basic framework in place with user registration, login, and then a page that you can upload images to MySql and those images are displayed below. What I am trying to do is create a system whereby when a user logs in they are sent to a page that shows a table with THEIR images that they have uploaded.. kinda creating the users very own page. I'm sure this is possible...
11
3825
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result = mysql_query("select * from $tableInfo") ; for ($i = 0; $i < mysql_num_rows($result); $i++) {
1
1714
by: kalyson | last post by:
Hello, I realize that using the TYPE declaration for variables makes their definitions more dynamic, like this: myField tablename.name%TYPE; myRow tablename%ROWTYPE; But I also want to be able to change the name of the table in the code. So I want to have "tablename" above refer to a VARIABLE that contains the name of a table, so I don't have to go changing the tablename throughout the code if the table name...
2
2855
by: ArizonaJohn | last post by:
Hello, Below I have some Ajax and the page it points to. This code works great if $_SESSION has no spaces in it (for example, if it is "elpaso"). However, if $_SESSION has a space in it (for example, "el paso"), then the Ajax fails. What can I do to make this Ajax work when $_SESSION has a space in it? Thanks. Ajax: <?php
6
7864
SBCUser666
by: SBCUser666 | last post by:
I have an Access 2003 query that does a SELECT.. INTO table_name. I would like the table_name to be a fixed value plus todays date. Example: tbl_EXTRACT_070709 Can this be done?
1
2907
by: smanif1 | last post by:
Report_Name_Final = "TABLENAME" DIM mySQL1 mySQL1 = "SELECT * FROM " mySQL1 = replace(mySQL1,"",Request.Form("Report_Name_Final")) response.write objRS1("Value") trying to get dynamic table name i getting this error
0
9417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9271
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8272
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4724
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4904
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2233
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.