Sql 根据Oracle中的字段值插入记录数

Sql 根据Oracle中的字段值插入记录数,sql,oracle,Sql,Oracle,我有以下脚本:- SELECT quoteid, tariff_length, cost FROM tblquotesnew q LEFT JOIN tbltariffsnew t ON q.tariff_id = t.tariff 这可能会返回如下结果:- quoteid tariff_length cost 310 4 12 311 6 16 是否可以将行插入一个单

我有以下脚本:-

SELECT
   quoteid,
   tariff_length,
   cost 
FROM
   tblquotesnew q 
   LEFT JOIN
      tbltariffsnew t 
      ON q.tariff_id = t.tariff
这可能会返回如下结果:-

quoteid tariff_length cost
310     4             12
311     6             16
是否可以将行插入一个单独的表中,其中插入的行数基于U长度

因此,使用上述方法,插入表tblcommnew如下所示

commid quoteid cost
1      310     12
2      310     12
3      310     12
4      310     12
5      311     16
6      311     16
7      311     16
8      311     16
9      311     16
10     311     16
这里有一个选择:

SQL> with test (quoteid, tariff_length, cost) as
  2    (select 310, 4, 12 from dual union
  3     select 311, 6, 16 from dual
  4    )
  5  select rownum as commid, quoteid, cost
  6  from test,
  7       table(cast(multiset(select level from dual
  8                           connect by level <= tariff_length
  9                          ) as sys.odcinumberlist));

    COMMID    QUOTEID       COST
---------- ---------- ----------
         1        310         12
         2        310         12
         3        310         12
         4        310         12
         5        311         16
         6        311         16
         7        311         16
         8        311         16
         9        311         16
        10        311         16

10 rows selected.

SQL>
这里有一个选择:

SQL> with test (quoteid, tariff_length, cost) as
  2    (select 310, 4, 12 from dual union
  3     select 311, 6, 16 from dual
  4    )
  5  select rownum as commid, quoteid, cost
  6  from test,
  7       table(cast(multiset(select level from dual
  8                           connect by level <= tariff_length
  9                          ) as sys.odcinumberlist));

    COMMID    QUOTEID       COST
---------- ---------- ----------
         1        310         12
         2        310         12
         3        310         12
         4        310         12
         5        311         16
         6        311         16
         7        311         16
         8        311         16
         9        311         16
        10        311         16

10 rows selected.

SQL>

@Littlefoot方法的一个细微变化是使用XMLTable生成组合:

with tblquotesnew (quoteid, tariff_length, cost) as (
            select 310, 4, 12 from dual
  union all select 311, 6, 16 from dual
)
select rownum as commid, quoteid, cost
from tblquotesnew
cross join xmltable ('1 to xs:integer($n)' passing tariff_length as "n");

    COMMID    QUOTEID       COST
---------- ---------- ----------
         1        310         12
         2        310         12
         3        310         12
         4        310         12
         5        311         16
         6        311         16
         7        311         16
         8        311         16
         9        311         16
        10        311         16
作为插入,您只需执行以下操作:

insert into tblcommnew (commid, quoteid, cost)
select rownum, quoteid, cost
from tblquotesnew
cross join xmltable ('1 to xs:integer($n)' passing tariff_length as "n");

10 rows inserted.

@Littlefoot方法的一个细微变化是使用XMLTable生成组合:

with tblquotesnew (quoteid, tariff_length, cost) as (
            select 310, 4, 12 from dual
  union all select 311, 6, 16 from dual
)
select rownum as commid, quoteid, cost
from tblquotesnew
cross join xmltable ('1 to xs:integer($n)' passing tariff_length as "n");

    COMMID    QUOTEID       COST
---------- ---------- ----------
         1        310         12
         2        310         12
         3        310         12
         4        310         12
         5        311         16
         6        311         16
         7        311         16
         8        311         16
         9        311         16
        10        311         16
作为插入,您只需执行以下操作:

insert into tblcommnew (commid, quoteid, cost)
select rownum, quoteid, cost
from tblquotesnew
cross join xmltable ('1 to xs:integer($n)' passing tariff_length as "n");

10 rows inserted.