通常、このようなことには動的 SQL を使用します。それには、DBMS_SQL
パッケージを使用するかEXECUTE IMMEDIATE
、OPEN <<cursor>> FOR <<SQL statement string>>
.
本当に静的 SQL を使用したい場合は、両方のテーブルにクエリを実行して、1 セットの結果のみを返すことができます。これが本当に理にかなっている状況を想像することはできませんが、あなたは確かにそれを行うことができます
FOO
とテーブルを作成しFOO2
ます。 FOO2
2行から1行まであります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
しかし、私が言ったように、これを行うことが実際に理にかなっている状況を理解することはできません.