通常,您会为这类事情使用动态 SQL。这将涉及使用DBMS_SQL 包、EXECUTE IMMEDIATE 或执行OPEN <<cursor>> FOR <<SQL statement string>>。
如果您真的想使用静态 SQL,您可以查询两个表并只返回一组结果。我无法想象这种情况会真正有意义,但你当然可以做到
创建一个FOO 和一个FOO2 表。 FOO2 有两行到FOO 的一行
SQL> create table foo( col1 number );
Table created.
SQL> create table foo2( col1 number );
Table created.
SQL> insert into foo values( 1 );
1 row created.
SQL> insert into foo2 values( 1 );
1 row created.
SQL> insert into foo2 values( 2 );
1 row created.
运行查询。这将返回来自FOO2的所有数据
SQL> ed
Wrote file afiedt.buf
1 select col1
2 from (select the_union.*,
3 max(cnt) over () max_cnt
4 from (select col1, count(*) over () cnt from foo
5 union all
6 select col1, count(*) over () from foo2) the_union)
7* where cnt = max_cnt
SQL> /
COL1
----------
1
2
在FOO 中插入更多行。现在同样的查询将返回来自FOO的所有数据
SQL> insert into foo values( 3 );
1 row created.
SQL> insert into foo values( 5 );
1 row created.
SQL> commit;
Commit complete.
SQL> select col1
2 from (select the_union.*,
3 max(cnt) over () max_cnt
4 from (select col1, count(*) over () cnt from foo
5 union all
6 select col1, count(*) over () from foo2) the_union)
7 where cnt = max_cnt;
COL1
----------
1
3
5
不过,正如我所说,我无法理解这样做实际上有意义的情况。