Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2006

    Unanswered: How do I generate dynamic table names in a SELECT statement?

    I need to generate the table name for the FROM clause on-the-fly. I can do it by building a string and then using that, but I really need to do it in a JOIN clause if possible.

    NOTE! I did NOT design this structure, it is an inherited project.

    Here is a basic example of what I need:

    select foo from CONCAT('regtable_',(select regtable from event where id = ?));
    The simplified schema looks like this:

    Table event:
      id - int,
      regtable - int
    Table regtable_1:
      regid - int,
      eventID - int,
      foo - varchar
    Table regtable_2:
      regid - int,
      eventID - int,
      foo - varchar
    Currently I have to select the regtable field from the event table, build a string from that and then use that string. I would like to do it all in a single SQL statement if possible. (And I am not sure at all that this is possible).

    Thanks in advance!


  2. #2
    Join Date
    Apr 2002
    Toronto, Canada
    i don't think it's possible | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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