• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Complex SQL Query

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two tables,

columns of first table contains Table name and column name of another table.

I want to retrieve one field from first table and one field from second table (but name of second table is not known until runtime as it is stored in first table)

Can anyone suggest how this can be done in one query if possible?

Look forward to an early reply
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you truly mean any table, I really don't believe this can be done in standard SQL. If you have an either/or situation, you might be able to craft a solution using outer joins.

It can however be done in many stored-procedure languages; just how depends on the stored procedure language your database supports, if any. (Stored procedures are called from JDBC using CallableStatement objects.)

Generally, such "dynamic" approaches can lead to various types of problems however; it often makes it tough to use PreparedStatement (or the appropriate parallel technique in the stored procedure language), and it often becomes hard to enforce referential integrity in the database.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, instead why not use ResultSet's existing methods to get the column names, types or even sizes? I don't see the advantages to store table/column names in one table for another table.
 
reply
    Bookmark Topic Watch Topic
  • New Topic