Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SQL dynamic table names

Posted on 2008-11-13
3
Medium Priority
?
181 Views
Last Modified: 2012-05-05
I have a database with tables that are named with the following format

priceHistory200812
priceHistory200811
priceHistory200810 etc....

Based upon a month that is chosen I need to retrieve data from the tables for the previous six months. How would I accomplish this in MSSQL?
0
Comment
Question by:acsakany
  • 2
3 Comments
 
LVL 3

Expert Comment

by:richard_crist
ID: 22961240
Since the tables are probably "backups" of the data for that time period I am assuming that the fields in each of those tables are the same as the original data table.  If that is the case then use can use a JOIN clause to collect all the tables' data together into one query result and then use a WHERE clause against the joined data to find the records in question.

Is there a reason that the data was broken into separate tables based on month?  At first glance it sounds like a good idea, but as you have found it can complicate data retrieval.

0
 

Author Comment

by:acsakany
ID: 22965117
I created a string with the "SELECT prichist200809 " in it and then I did an EXEC(@sqlString). This worked.
0
 
LVL 3

Accepted Solution

by:
richard_crist earned 200 total points
ID: 22968186
Building a dynamic string works for data within one specific table.  If you need data from more than one table month you can use the join or you can use the string method you mention above to query multiple tables and gather the data together manually.
If you found the answer yourself you can choose to have the question closed or if my comment helped you can accept my solution.  Let us know if you need any other information and I or others will try to help.   :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question