2014-10-21 2 views
3

У меня есть оператор вставки, подобную этой:Вставка же данные несколько раз

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

мне нужно вставить этот же оператор с несколькими идентификаторами. Прямо сейчас у меня есть:

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'); 

Мне просто нужно запустить это, или есть более сжатая версия?

ответ

7

Используйте 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; 
2

Вы можете использовать INSERT ALL заявление

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; 
1
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') 
0

Ради аргумента, можно было бы создать более постоянного решения, если это ID также primary_key, создав последовательность, добавив в таблицу триггер ДОПОЛНИТЕЛЬ ВСТАВКИ, чтобы автоматически увеличивать идентификатор с помощью последовательности, затем цикл, вставляя, однако, сколько строк, и пусть идентификатор сам прирост:

-- 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;