如何为唯一 ID 插入多行

How to insert multiple rows for a unique ID

我正在尝试在 DB 中实现此插入结果:

USER ID    SERVICE  OPERATION

2681    6        47
2681    6        48
2681    6        95
2681    6        104
2681    12       106
2681    12       116

对于 3000 个唯一用户 ID,其中每个用户 ID 的第二列和第三列数据相同。

我可以

INSERT INTO table (user, service, operation)
   VALUES ('2681', '6', '47'); ('2681', '6', '48'); etc..

并对 18000 行重复此操作。

有更简单的方法吗?

你或许可以使用插入 select:

insert into my table (user, service, operation) 
with sop(service, operation) as
(
  select  6,  47 from dual union all
  select  6,  48 from dual union all
  select  6,  95 from dual union all
  select  6, 104 from dual union all
  select 12, 106 from dual union all
  select 12, 116 from dual
)
select users.user, sop.service, sop.operation
from sop
cross join users;

您可以使用 INSERT INTO ... SELECT 和一个集合来缩短代码:

INSERT INTO table_name ( user, service, operation )
SELECT '2681', '6', op.COLUMN_VALUE
FROM   TABLE( SYS.ODCINUMBERLIST( 47, 48, 95, 104 ) ) op;

如果您愿意,可以交叉连接多个集合:

INSERT INTO table_name ( user, service, operation )
SELECT usr.COLUMN_VALUE,
       srv.COLUMN_VALUE,
       op.COLUMN_VALUE
FROM   TABLE( SYS.ODCIVARCHAR2LIST( '2681', '2682' ) ) usr,
       TABLE( SYS.ODCIVARCHAR2LIST( '6', '12' ) ) srv,
       TABLE( SYS.ODCINUMBERLIST( 47, 48, 95, 104 ) ) op;