"I don't have the name of the table
right away! They're stored in another
table"
Oracle doesn't do this sort of thing in SQL. You'll need to use PL/SQL and assemble a dynamic query.
create or replace function get_dynamic_rows
return sys_refcursor
is
stmt varchar2(32767) := null;
return_value sys_refcursor;
begin
for r in ( select table_name from your_table )
loop
if stmt is not null then
stmt := stmt||' union all ';
end if;
stmt := stmt||'select * from '||r.table_name;
end loop;
open return_value for stmt;
return return_value;
end;
/
This will assemble a query like this
select * from table_1 union all select * from table_2
The UNION ALL is a set operator which combines the output of several queries in a single result set without removing duplicates. The columns in each query must match in number and datatype.
Because the generated statement will be executed automatically there's no real value in formatting it (unless the actual bits of the query are more complicated and you perhaps need to debug it).
Ref Cursors are PL/SQL contructs equivalent to JDBC or .Net ResultSets. Find out more.