且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建具有自动提交功能的oracle表

更新时间:2023-02-03 15:53:18

可以使用自主交易.

CREATE TABLE t1 (x INTEGER PRIMARY KEY);
CREATE TABLE t2 (x INTEGER);
CREATE TABLE t3 (x INTEGER);
CREATE TABLE t4 (x INTEGER REFERENCES t1(x));

然后运行此匿名PL/SQL.仔细查看,本地过程upd_table_3在声明时未运行.在主BEGIN块中调用时运行:

Then run this anononymous PL/SQL. Look carefully, the local procedure upd_table_3 isn't run when it's declared. It's run when called within the main BEGIN block:

DECLARE 
  PROCEDURE upd_table_3 IS
  PRAGMA AUTONOMOUS_TRANSACTION;
  BEGIN
    INSERT INTO t3 VALUES (3);
    COMMIT;
  END;
BEGIN
  INSERT INTO t1 VALUES (1);
  INSERT INTO t2 VALUES (2);
  upd_table_3;
  INSERT INTO t4 VALUES (4);
  COMMIT;
END;
/

在第四个插入ORA-02291的过程中,该过程应故意失败.我是这样做的.然后

The procedure should fail intentionally on the 4th insert with ORA-02291. I made it this way. Then

ROLLBACK;

t3以外,所有表都应回滚,我们在自动事务中进行了插入.

All the tables should be rolled back except t3, which we did the insert in an autonomous transaction.

SELECT * FROM t1;

no rows selected

SELECT * FROM t2;

no rows selected

SELECT * FROM t3;

         X
----------
         3

SELECT * FROM t4;

no rows selected