如何为唯一ID插入多行

时间:2016-06-01 08:44:28

标签: sql oracle insert rows

我正在尝试在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行。

有更简单的方法吗?

2 个答案:

答案 0 :(得分:1)

您可以使用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;

答案 1 :(得分:0)

您可以使用插入选择:

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;