SQL dynamic table names

Posted on 2008-11-13
Last Modified: 2012-05-05
I have a database with tables that are named with the following format

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?
Question by:acsakany
    LVL 3

    Expert Comment

    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.


    Author Comment

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

    Accepted Solution

    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.   :)

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Left Justify field in Oracle 6 54
    Number Format 1 31
    SQL Select Query problems 10 37
    monitor queries that use too much tempdb log 20 14
    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now