Search code examples
javasqlresultsetmultiple-resultsets

Is it possible in Java to retrieve tables of unrelated data in a single SQL query and resultset?


We have multiple tables that contain "static" key/value pairs, which are currently pulled using multiple SQL (MSSQL) queries. Is it possible to pull all of this data in one SQL query, so that we can reference each column key and column value in a single result set? So for example:

TABLE_ONE
id, my_key_name, my_value_name

TABLE_TWO
id, my_other_key_name, my_other_value_name

Keep in mind that the column names for the key and for the value are different for each table. Basically, we're trying to consolidate multiple calls into one. Is this a situation where we'll have to have multiple Java ResultSets and we'll just need to do the combining in the code?


Solution

  • How about

    SELECT id, my_key_name, my_value_name
    FROM   TABLE_ONE
    UNION
    SELECT id, my_other_key_name, my_other_value_name
    FROM   TABLE_TWO
    

    ?

    See: UNION