Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2011
    Posts
    18

    Question Unanswered: Create generic query for table with generic name

    Hi guys,

    I have about 20 tables with a generic name like :
    table_1
    table_2
    table_3
    table_4
    table_5
    ...
    table_20

    I need to execute the same query on each table. As I'm a bit tired to copy paste the same thing 20 times in a text file .. Can I do something like a for loop or whatever that does the same and would execute my query

    Code:
    i=1
    for i [1, 20] {
    go to table_i ... do the query
    i=+1
    }
    I know it might be confusing .. but it's hard to explain sorry

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    frankly that stinks of bad design
    20+ tables with a similar structure
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by madkitty View Post
    As I'm a bit tired to copy paste the same thing 20 times in a text file ..
    oh, you should have thought of this when you designed 20 identical tables

    of course, redesigning them is probably a lot more work than copy/paste, isn't it...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    Yes it is possible but as the others say it does seem that your design is not ideal. Have a look at stored procedures and in particular to PREPARE, EXECUTE and DEALLOCATE PREPARE which allows you do dynamically create statements.
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  5. #5
    Join Date
    Apr 2011
    Posts
    18
    yeah i know the design sucks but I have no other option. Our data are over 6GB per table, I couldn't figure out any other design so far ..

    I'll read more on the prepare/execute/deallocate prepare statements.
    Thanks for your help~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •