Database -" oracle data table batch insertion (anti-primary key increment conflict)

--Move the data of table A to table B. Assuming that both table A and table B have key1 and key2 fields, here is just an example to move the values ​​of key1 and key2
DECLARE
  i int := 0; - define a counter
  newbasicobjid int: = 0;--Define the largest id of table B
  oldtablecoount int:= 0;--Define the data volume of table A
  oldmaxobjid int:= 0;--Define the largest id of table A
  oldkey1 varchar2(100) :='';-- Define a temporary variable of type varchar2 to store the first field of the A table
  oldkey2 int := 0;-define a temporary variable of type int to store the second field of the A table

BEGIN
  --Start select max(id) into newbasicobjid from table_B; --Query the largest id of table B and assign it to the variable
  select count(*) into oldtablecoount from table_A;--Query the amount of data in table A and assign it to the variable
  select max (id) into oldmaxobjid from table_A; --Query the largest id of table A and assign it to the variable
  for i in 1 .. oldtablecoount loop --for loop loop range 1 to the amount of data in table A
    select key1,
           key2,
      into oldkey1,
           oldkey2
      from table_A - Find out the actual field value from the A table and assign it to the temporary variable
     where id = (oldmaxobjid + 1-i); - In order to prevent primary key conflicts, one by one query can be achieved through this where condition
    newbasicobjid := newbasicobjid + 1;--for once in the for loop, the id+1 of the new table will be inserted to achieve the effect of primary key self-growth
    insert into table_B - start inserting data, the number of fields in values ​​must be the same as the number of fields inserted into table B Consistent       
      (id,
       key1,
       key2)
    values
      (newbasicobjid,
       oldkey1,
       oldkey2);
  end loop;
  commit; --submit
END; --end

Guess you like

Origin blog.csdn.net/nienianzhi1744/article/details/90229647