Is there a way to bulk copy the contents of one variable of a PL / SQL array to another?

Let's say I have two table variables of the same type in my package. Note that these types are local to the package.

TYPE MyTableType is table of some_table%ROWTYPE; table1 MyTableType; table2 MyTableType; 

And I have a procedure in a package that loads some data into tables1 and table2. At some point after this, I want to add everything currently to table1 to table2.

Is there a better way to do this than loop table1 and .extend -ing table2, and then set the value of the .last element on each iteration?

I'm looking for some kind of quick mass operation, if it exists. It?

+4
source share
1 answer
 22:02:22 SYSTEM@dwal > ed Wrote file S:\spool\dwal\BUFFER_SYSTEM_386.sql 1 declare 2 type t is table of dual%rowtype; 3 c1 t := t(); 4 c2 t := t(); 5 begin 6 c1.extend; 7 c1(1).dummy := 'a'; 8 c2.extend(2); 9 c2(1).dummy := 'b'; 10 c2(2).dummy := 'c'; 11 c2 := c1 multiset union all c2; 12 for i in c2.first .. c2.last loop 13 dbms_output.put_line(c2(i).dummy); 14 end loop; 15* end; 22:02:41 SYSTEM@dwal > / a b c PL/SQL procedure successfully completed. Elapsed: 00:00:00.26 

upd: I never had the opportunity to test multiposition operations, aaaaaand, although they are in bulk, they seem to actually be slower:

 22:14:56 SYSTEM@dwal > ed Wrote file S:\spool\dwal\BUFFER_SYSTEM_331.sql 1 declare 2 cnt int := 1e5; 3 type t is table of dual%rowtype; 4 c1 t := t(); 5 c2 t := t(); 6 timer int; 7 procedure prebuild as 8 begin 9 c1.delete; 10 c2.delete; 11 c1.extend(cnt); 12 c2.extend(cnt); 13 for i in 1 .. cnt loop 14 c1(i).dummy := dbms_random.string('l', 1); 15 c2(i).dummy := dbms_random.string('l', 1); 16 end loop; 17 end; 18 begin 19 -- 1 20 prebuild; 21 timer := dbms_utility.get_cpu_time; 22 for i in 1 .. cnt loop 23 c2.extend; 24 c2(c2.last) := c1(i); 25 end loop; 26 dbms_output.put_line(dbms_utility.get_cpu_time - timer); 27 -- 2 28 prebuild; 29 timer := dbms_utility.get_cpu_time; 30 c2 := c2 multiset union all c1; 31 dbms_output.put_line(dbms_utility.get_cpu_time - timer); 32 -- 3 33 prebuild; 34 timer := dbms_utility.get_cpu_time; 35 c2.extend(c1.count); 36 for i in 1 .. cnt loop 37 c2(c2.count - c1.count + i) := c1(i); 38 end loop; 39 dbms_output.put_line(dbms_utility.get_cpu_time - timer); 40* end; 22:15:00 SYSTEM@dwal > / 15 25 10 PL/SQL procedure successfully completed. Elapsed: 00:00:03.38 

These numbers are pretty stable. The third approach, which extends the second collection only once and then adds to it, seems to be the fastest.

+5
source

Source: https://habr.com/ru/post/1501308/


All Articles