简体   繁体   English

FORALL插入目标表,然后从源表中删除FORALL

[英]FORALL insert destination table then a FORALL delete from source table

Cam the below be done without issues? Cam下面没有问题吗? Where as soon as you insert a record into the destination table you can then delete that record from the source table. 只要您将记录插入目标表,就可以从源表中删除该记录。 Obviously this record is in memory within the loop, can you see any problems with this or can it be done in a different way. 显然这条记录在循环内存中,你能看到任何问题,还是可以用不同的方式完成。

I know all of you will say just do a direct SQL insert with APPEND and then truncate the source table. 我知道你们所有人都会说用APPEND做一个直接的SQL插入,然后截断源表。

I'm just throwing the question out there as I'm curious. 因为我很好奇,我只是把问题扔出去了。

PROCEDURE copy_records_back IS

  TYPE t_act_plus_triggers_copy1 IS TABLE OF act_plus_triggers_copy1%ROWTYPE;   v_act_plus_triggers_copy1 t_act_plus_triggers_copy1;

  CURSOR c_act_plus_triggers_copy1 IS   SELECT * FROM act_plus_triggers_copy;


BEGIN

  EXECUTE IMMEDIATE 'TRUNCATE TABLE act_plus_triggers1';

  OPEN c_act_plus_triggers_copy1;   LOOP
    FETCH c_act_plus_triggers_copy1 BULK COLLECT INTO v_act_plus_triggers_copy LIMIT 10000;  

    FORALL i IN 1..v_act_plus_triggers_copy.COUNT  
      INSERT /*+ APPEND_VALUES */ INTO act_plus_triggers1  values v_act_plus_triggers_copy(i);       


    FORALL i IN 1..v_act_plus_triggers_copy.COUNT  
    DELETE FROM act_plus_triggers_copy
    where surr_id = v_act_plus_triggers_copy(i).surr_id

    COMMIT;     
    EXIT WHEN c_act_plus_triggers_copy1%NOTFOUND;

    END LOOP;   
CLOSE c_act_plus_triggers_copy1;

END copy_records_back;

If the two tables have the same columns, there is a simpler method to do this without copying the data at all. 如果两个表具有相同的列,则有一种更简单的方法可以完成此操作而无需复制数据。 You can create a partitioned table with the same columns as both tables and with only one partition. 您可以使用与两个表相同的列创建分区表,并且只创建一个分区。 Then you can use ALTER TABLE EXCHANGE PARTITION to swap the table with the partition, then you can swap in the same way the partition with the target table (if one of the tables is partitioned, you only need to do one swap). 然后,您可以使用ALTER TABLE EXCHANGE PARTITION将表与分区交换,然后您可以以与目标表相同的方式交换分区(如果其中一个表已分区,则只需要进行一次交换)。 If the data need to change tablespace (or some other physical properties), you can issue ALTER TABLE MOVE at the end to accomplish this task. 如果数据需要更改表空间(或其他一些物理属性),则可以在最后发出ALTER TABLE MOVE来完成此任务。

In your situation the statements for swapping two tables will look like: 在您的情况下,交换两个表的语句如下所示:

-- initialization (do only once)
CREATE TABLE temp_part_table PARTITION BY HASH (surr_id) (PARTITION single) AS
SELECT * FROM act_plus_triggers_copy WHERE 1=0
/
-- swapping act_plus_triggers_copy with act_plus_triggers1
ALTER TABLE temp_part_table EXCHANGE PARTITION single WITH TABLE act_plus_triggers1
WITHOUT VALIDATION
/
ALTER TABLE temp_part_table EXCHANGE PARTITION single WITH TABLE act_plus_triggers_copy
WITHOUT VALIDATION
/
ALTER TABLE temp_part_table EXCHANGE PARTITION single WITH TABLE act_plus_triggers1
WITHOUT VALIDATION
/

There are three exchanges if you want to swap both tables. 如果要交换两个表,则有三个交换。 If you know that both temp_part_table (temporary partitioned table) and act_plus_triggers1 (target table) are initially empty, you can skip the first ALTER . 如果您知道temp_part_table (临时分区表)和act_plus_triggers1 (目标表)最初都为空,则可以跳过第一个ALTER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 效果:删除与正式 - Performance: DELETE vs FORALL INSERT到另一个表,然后从源表中删除 - INSERT into another table, and then DELETE from the source table MySQL从2个源表插入到一个目标表 - MySQL Insert from 2 source tables to one destination table 使用FORALL和RETURNING插入表时如何获取ROWID - How to get ROWID when inserting into table using FORALL and RETURNING 如果目标表sql中没有源值,则将其插入到目标表中 - Insert into a destination table if the source value is not there in destination table sql forall插入需要更多时间插入 - forall insert taking more time to insert SQL:将数据从源表复制到目标表后,更新源表中的值(来自目标表) - SQL:Updating a value (coming from destination table) in the source table after copying the data from source to destination table 带有动态查询和表名的 BULK COLLECT/FORALL 语句 - Oracle PL/SQL - BULK COLLECT/FORALL statements with dynamic query and table name- Oracle PL/SQL 2要求:1)用FORALL替换多个FOR循环2)在将数据插入表中时使用IF-THEN-ELSE条件 - 2 requirements: 1) Replace multiple FOR loop with FORALL 2) use IF-THEN-ELSE condition while inserting data into table 使用50个并行SQLPLUS会话以及批量收集LIMIT和FORALL的500多个记录的UPDATE表 - UPDATE Table with 500+ Millions of Records using 50 Parallel SQLPLUS Sessions with Bulk Collect LIMIT and FORALL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM