6

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?

O P
  • 2,327
  • 10
  • 40
  • 73

4 Answers4

9

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;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
2

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;
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
2
INSERT INTO [TableName] (id, name, descr) VALUES 
(4, 'asdf', 'this is not a word'),
(6, 'asdf', 'this is not a word'),
(7, 'asdf', 'this is not a word'),
(9, 'asdf', 'this is not a word')
ccStars
  • 817
  • 2
  • 11
  • 34
0

For the sake of argument, one could create a more permanent solution if that ID is also the primary_key by creating a sequence, adding a BEFORE INSERT trigger to the table to increment the ID using the sequence automatically, then loop, inserting however many rows you want and let the ID increment itself:

-- Create the table    
CREATE TABLE SEQ_TEST
(
  ST_ID    NUMBER,
  ST_NAME  VARCHAR2(50 BYTE),
  ST_DESC  CHAR(100 BYTE)
);

-- Create the sequence
CREATE SEQUENCE SEQ_TEST_SEQ
  START WITH 1
  MAXVALUE 9999999999999999999999999999
  MINVALUE 0
  NOCYCLE
  NOCACHE
  ORDER;

-- Create the before insert trigger
CREATE OR REPLACE TRIGGER SEQ_TEST_BI
BEFORE INSERT
ON SEQ_TEST
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN

  if :old.ST_ID is null then
    :new.ST_ID := SEQ_TEST_SEQ.nextval;
  end if;

END SEQ_TEST_BI;

-- insert 25 rows using an anonymous block.  Note the ID is NULL 
--  which causes the trigger to increment ID
--  based on the sequence.
begin
  for i in 1..25
  loop
    -- NOTE - Technically you could omit the 'ST_ID' and NULL and it would
    --        still work, but I prefer to keep them here to show this action 
    --        of inserting NULL is intentional and show that all columns are
    --        accounted for in the insert.  
    insert into SEQ_TEST (ST_ID, ST_NAME, ST_DESC) values (NULL, 'asdf', 'this is not a word');
  end loop;
end;

commit;

-- Prove it.
select * from seq_test;
Gary_W
  • 9,933
  • 1
  • 22
  • 40