Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert same data multiple times

Tags:

sql

oracle

plsql

I have an insert statement similar to this:

insert into table (id, name, descr) values (4, 'asdf', 'this is not a word');

I need to insert this same statement with multiple ids. Right now I have:

insert into table (id, name, descr) values (4, 'asdf', 'this is not a word');
insert into table (id, name, descr) values (6, 'asdf', 'this is not a word');
insert into table (id, name, descr) values (7, 'asdf', 'this is not a word');
insert into table (id, name, descr) values (9, 'asdf', 'this is not a word');

Am I just going to have to run this, or is there a more condensed version?

like image 571
O P Avatar asked Dec 20 '22 10:12

O P


2 Answers

Use a select . . . insert:

insert into table(id, name, descr) 
    select i.id, 'asdf', 'this is not a word'
    from (select 4 as id from dual union all
          select 6 from dual union all
          select 7 from dual union all
          select 9 from dual
         ) i;
like image 132
Gordon Linoff Avatar answered Jan 13 '23 19:01

Gordon Linoff


You can use the INSERT ALL statement

INSERT ALL
  INTO table (id, name, descr) VALUES (4, 'asdf', 'this is not a word')
  INTO table (id, name, descr) VALUES (6, 'asdf', 'this is not a word')
  INTO table (id, name, descr) VALUES (7, 'asdf', 'this is not a word')
  INTO table (id, name, descr) VALUES (9, 'asdf', 'this is not a word')
SELECT * FROM dual;
like image 25
Wernfried Domscheit Avatar answered Jan 13 '23 17:01

Wernfried Domscheit