【发布时间】:2019-06-10 13:33:20
【问题描述】:
我需要编写一个在表中插入数千行的过程,并使用这些行生成的自动生成的 id 并将其用于其他插入。
我使用了一个 for 循环,其中我将序列 id 保存在一个变量中,然后在我的插入中使用它。
declare
first_id integer;
BEGIN
FOR texts in (select distinct text from table_texts )
LOOP
first_id := SEQ_IDS_OBJECTID.NEXTVAL;
INSERT INTO table_1(id,some_fields)
VALUES (first_id, 'blablabla');
insert into table_2 (id,text_field)
VALUES (first_id, texts.text);
END LOOP;
commit;
END;
我认为这不是实现我需要的理想方式。此外,当我在 TOAD 中输入代码时,我收到以下警告: 规则 4809(包含 DML 语句的循环应重构为使用 BULK COLLECT 和 FORALL)
有没有更好的办法?
编辑:
上面的代码被简化了。但我想我必须揭露更多来解释这个案子:
declare
first_id integer;
second_id integer;
BEGIN
FOR texts in (select distinct text1 , text2 from mdf )
LOOP
first_id := XAKTA.SEQ_IDS_OBJECTID.NEXTVAL;
select id_1 into second_id from table_3 where field_1 =texts.text1 ;
INSERT INTO table_1(id_1,id_2,some_fields)
VALUES (first_id ,second_id ,'blablabla');
insert into table_2 (id,text1,text2)
VALUES (first_id, texts.text1,texts.text2);
END LOOP;
commit;
END;
【问题讨论】: