user3224907
user3224907

Reputation: 768

oracle - How to insert unique values from a table to another multiple times?

Using Oracle 12c, I have, for example, 5 rows with values 1,2,3,4,5 in a PK column in one table TABLEA. I would like to insert into another table TABLEB the values but 3 times. So TABLE would have 15 rows with values 1,1,1,2,2,2,3,3,3,4,4,4,5,5,5 after the insert. How can I achieve this?

I'm trying to create a script that will insert values from TABLEA to TABLEB if they don't already exist there. Currently I am manually inserting into TABLEB each value from TABLEA 3 times.

Upvotes: 0

Views: 1100

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269693

You can use cross join. The query would look something like this:

insert into t(pk)
    select pk
    from table t2 cross join
         (select 1 as n from dual union all select 2 from dual union all select 3 from dual
         ) n;

Upvotes: 1

Related Questions