oracle ORA-12838如何在删除和插入之前预测试查询?

au9on6nz  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(137)

错误:ORA-12838:在并行修改对象后,无法读取/修改对象。当然,提交删除会破坏目的。
是否有一种方法可以使用未提交的删除,然后插入,以便在出现问题时回滚,例如。插入写?
代码:

DECLARE
    tbl_count number;
    sql_stmt long;

BEGIN
    SELECT 
        COUNT(*) 
    INTO 
        tbl_count  
    FROM 
        ALL_TABLES   
    WHERE
        table_name = 'XXX';

IF(tbl_count <= 0) THEN
  sql_stmt:=
'CREATE TABLE XXX (
                   AA varchar2(255),
                   BB DATE
                   )';

EXECUTE IMMEDIATE sql_stmt;

END IF;

END;

BEGIN
    EXECUTE IMMEDIATE 'DELETE FROM XXX';

INSERT INTO
    XXX
SELECT
    "AA",
    TO_DATE("BB",'YYYY-MM-DD') AS BB
FROM
    "XXX_STG";

COMMIT;

EXCEPTION 
    WHEN OTHERS THEN
      ROLLBACK;
      RAISE;
END;

它在概念上还没有意义,将感谢一些帮助/想法。

wsxa1bj1

wsxa1bj11#

ORA-12838与事务本身无关,但与使用 parallel dml修改表有关。如果您不使用并行DML,那么您可以在同一事务中执行DELETE-then-INSERT,而不会出现任何问题。

SQL> create table t as
  2  select * from dba_objects;

Table created.

SQL>
SQL> begin
  2    delete from t;
  3    insert into t
  4    select * from dba_objects;
  5    commit;
  6  end;
  7  /

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> alter session force parallel dml;

Session altered.

SQL> begin
  2    delete from t;
  3    insert into t
  4    select * from dba_objects;
  5    commit;
  6  end;
  7  /
begin
*
ERROR at line 1:
ORA-12838: cannot read/modify an object after modifying it in parallel
ORA-06512: at line 3

如果您希望保持并行处理,并且还保留已删除行的记录“以防万一”,可以执行以下操作

  • 创建backup_table为select * from table
  • 截短表
  • 插入到表中选择...

这项限制在23 c btw取消。

相关问题