使用交叉连接的 CTE 可能会有所帮助。方法如下。
先创建表和序列;我的 base 表仅包含 3 行(与您的 68 行相反;只是为了简单起见。您不必创建 base 表,因为您已经拥有它):
SQL> create table base as
2 select 1 base_id from dual union all
3 select 2 base_id from dual union all
4 select 3 base_id from dual;
Table created.
SQL> create sequence seqankit;
Sequence created.
SQL> create table type (type_id number, type_name varchar2(20), base_id number);
Table created.
SQL> create table operation (operation_id number, operation_name varchar2(20), base_id number);
Table created.
SQL>
插入type 和operation 表:
SQL> insert into type (type_id, type_name, base_id)
2 with names (type_name) as
3 (select 'type1' from dual union all
4 select 'type2' from dual union all
5 select 'unknown' from dual
6 )
7 select seqankit.nextval, n.type_name, b.base_id
8 from names n cross join base b;
9 rows created.
SQL> insert into operation (operation_id, operation_name, base_id)
2 with names (operation_name) as
3 (select 'operation1' from dual union all
4 select 'operation2' from dual union all
5 select 'unknown' from dual
6 )
7 select seqankit.nextval, n.operation_name, b.base_id
8 from names n cross join base b;
9 rows created.
SQL>
结果如何?
SQL> select * from type order by base_id, type_name;
TYPE_ID TYPE_NAME BASE_ID
---------- -------------------- ----------
1 type1 1
4 type2 1
7 unknown 1
2 type1 2
5 type2 2
8 unknown 2
3 type1 3
6 type2 3
9 unknown 3
9 rows selected.
SQL> select * from operation order by base_id, operation_name;
OPERATION_ID OPERATION_NAME BASE_ID
------------ -------------------- ----------
10 operation1 1
13 operation2 1
16 unknown 1
11 operation1 2
14 operation2 2
17 unknown 2
12 operation1 3
15 operation2 3
18 unknown 3
9 rows selected.
SQL>
我觉得不错。