sql - Oracle Select query with dynamic table name -
i m trying create complicated select query uses temp tables , syntax below
with table1 ( select start_date, end_date ....somequery - returns 1 row ), table2 ( select ... somequery use table1 columns in clause.. ), table3 ( select ... use table1 columns in clause .. ) select * select case when ( start_date < sysdate -1000 , end_date > sysdate ) 'table2' else 'table3' end table1 rownum < 10
so logic simple based on return value table1 may want query table 2 or may want query table3
problem: oracle doesnt allow table name dynamically generated in sql query
i know can write procedure , use execute immediate reason have via single query. appreciated.
you can main select
:
select * table1 cross join table2 start_date < sysdate - 1000 , end_date > sysdate , rownum < 10 union select * table1 cross join table3 not (start_date < sysdate - 1000 , end_date > sysdate) , rownum < 10
the idea use union all
2 conditions , where
guarantee no rows returned subject conditions. note form of where
statement not take account null
values start_date
or end_date
.
i suspect there might way write query, if more details table2
, table3` available.
Comments
Post a Comment