[英]Bulk insertion of records using BULK COLLECT and FORALL
I am a beginner to PL/SQL and fetching huge amount of records from different tables and want to insert in to a one single table in PL/SQL using anonymous block with BULK COLLECT and FORALL. 我是PL / SQL的初学者,并且从不同的表中获取大量记录,并希望使用带有BULK COLLECT和FORALL的匿名块将其插入PL / SQL中的单个表中。 Could anybody help me out in deciding whether the following code is correct.
任何人都可以帮助我确定以下代码是否正确。 I have referred many links
我提到了很多链接
http://uksanjay.blogspot.com/2012/08/difference-between-bulk-collect-and.html?m=1 http://uksanjay.blogspot.com/2012/08/difference-between-bulk-collect-and.html?m=1
How do I use bulk collect and insert in Pl/SQl 如何在Pl / SQl中使用批量收集和插入
There are two tables 'ABC' and 'BCD' from which records are fetched and inserted in to a destination table 'DEF' 有两个表“ ABC”和“ BCD”,从中获取记录并将其插入到目标表“ DEF”中
ABC table (A,B,C ARE COLUMN NAMES) ABC表 (A,B,C是列名)
A B C
1 X Z1
2 Y Z2
BCD TABLE BCD表
A B C
1 X Z1
2 Y Z2
In destination table 'DEF' I have to insert both the table records. 在目标表“ DEF”中,我必须插入两个表记录。
code is as follows: 代码如下:
DECLARE
TYPE FETCH_ARRAY IS TABLE OF A_CUR%ROWTYPE;
A_ARRAY FETCH_ARRAY;
CURSOR A_CUR IS
SELECT * FROM ABC
UNION ALL
SELECT * FROM BCD;
BEGIN
OPEN A_CUR;
LOOP
FETCH A_CUR BULK COLLECT INTO A_ARRAY LIMIT 1000;
FORALL i IN 1..A_ARRAY.COUNT
INSERT INTO DEF VALUES A_ARRAY(i);
EXIT WHEN A_CUR%NOTFOUND
END LOOP;
CLOSE A_CUR;
COMMIT;
END;
PS: select statements are more complex in the actual code, and source tables 'ABC' and 'DEF' consists of million records. PS:select语句在实际代码中更为复杂,源表“ ABC”和“ DEF”包含数百万条记录。 So kindly help me in writing an efficient code.
因此,请帮助我编写高效的代码。
The optimal solution would be to rewrite your PL/SQL code into a single SQL INSERT INTO SELECT statement, like this: 最佳解决方案是将PL / SQL代码重写为单个SQL INSERT INTO SELECT语句,如下所示:
INSERT INTO def
SELECT * FROM abc
UNION ALL
SELECT * FROM bcd;
Note: if there exist some same records in both abc
and bcd
tables and you want only 1 record to be inserted in that situation then use UNION
instead of UNION ALL
. 注意:如果
abc
和bcd
表中都存在一些相同的记录,并且您只想在这种情况下插入1条记录,则使用UNION
而不是UNION ALL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.