This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes Complex SQL Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Complex SQL Query" Watch "Complex SQL Query" New topic
Author

Complex SQL Query

Ishu Sharma
Greenhorn

Joined: Apr 18, 2005
Posts: 21
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


I'd rather be a could-be if I cannot be an are; because a could-be is a maybe who is reaching for a star.
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
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.
Ben Zung
Ranch Hand

Joined: Mar 25, 2004
Posts: 109
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Complex SQL Query