Oracle中的触发器以及回滚后如何保留记录

发布于 2024-12-14 16:08:54 字数 842 浏览 2 评论 0原文

我需要创建一个触发器,将更改写入影子表中。我知道如何创建触发器,但我的挑战是,即使在回滚之后,我也需要新表中的记录仍然存在。

这是输出的示例

INSERT INTO department VALUES (95, 'PURCHASING', 'CHICAGO');<br>
ROLLBACK;

1 rows inserted.
rollback complete.

SELECT * FROM department_log;

DEPARTMENT_ID           DEPARTMENT_NAME       ADDRESS               OPERATION_TIME            
---------------------- -------------------- -------------------- ------------------ 
90                      HR                    CHICAGO               03-NOV-11
95                      PURCHASING            CHICAGO               03-NOV-11

SELECT * from department WHERE department_id >= 90;

DEPARTMENT_ID           DEPARTMENT_NAME       ADDRESS              
---------------------- -------------------- -------------------- 
90                      HR                    CHICAGO

I need to create a trigger that writes changes in a shadow table. I know how to create the trigger but my challenge is that I need the records in the new table to exist even after a rollback.

This is an example of how the output will look like

INSERT INTO department VALUES (95, 'PURCHASING', 'CHICAGO');<br>
ROLLBACK;

1 rows inserted.
rollback complete.

SELECT * FROM department_log;

DEPARTMENT_ID           DEPARTMENT_NAME       ADDRESS               OPERATION_TIME            
---------------------- -------------------- -------------------- ------------------ 
90                      HR                    CHICAGO               03-NOV-11
95                      PURCHASING            CHICAGO               03-NOV-11

SELECT * from department WHERE department_id >= 90;

DEPARTMENT_ID           DEPARTMENT_NAME       ADDRESS              
---------------------- -------------------- -------------------- 
90                      HR                    CHICAGO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟沫凡尘 2024-12-21 16:08:57

您需要将触发器声明为自治事务,

PRAGMA AUTONOMOUS_TRANSACTION;

这将触发器代码与主事务解耦,因此即使表中的主插入(触发触发器)回滚,触发器也会在不同的事务上下文中执行,并且可以提交/ 独立回滚。

You would need to declare the trigger as an Autonomous Transaction

PRAGMA AUTONOMOUS_TRANSACTION;

This decouples the trigger code from the main transaction, so even if the main insertion into the table (which fired the trigger) rollsback, the trigger is executed in a different transactional context and can commit / rollback independently.

您需要使用自主事务。

SQL> create table t (col1 number);

Table created.

SQL> create table t_shadow( col1 number, dt date );

Table created.

SQL> create trigger trg_t
  2    before insert on t
  3    for each row
  4  declare
  5    pragma autonomous_transaction;
  6  begin
  7    insert into t_shadow( col1, dt )
  8      values( :new.col1, sysdate );
  9    commit;
 10  end;
 11  /

Trigger created.

SQL> insert into t values( 1 );

1 row created.

SQL> rollback;

Rollback complete.

SQL> select * from t;

no rows selected

SQL> select * from t_shadow;

      COL1 DT
---------- ---------
         1 09-NOV-11

请注意,如果您发现自己将自主事务用于持久日志记录以外的任何用途,那么几乎肯定您做错了什么。自主交易是一项非常危险且经常被滥用的功能。

You'll need to use autonomous transactions.

SQL> create table t (col1 number);

Table created.

SQL> create table t_shadow( col1 number, dt date );

Table created.

SQL> create trigger trg_t
  2    before insert on t
  3    for each row
  4  declare
  5    pragma autonomous_transaction;
  6  begin
  7    insert into t_shadow( col1, dt )
  8      values( :new.col1, sysdate );
  9    commit;
 10  end;
 11  /

Trigger created.

SQL> insert into t values( 1 );

1 row created.

SQL> rollback;

Rollback complete.

SQL> select * from t;

no rows selected

SQL> select * from t_shadow;

      COL1 DT
---------- ---------
         1 09-NOV-11

Note that if you find yourself using autonomous transactions for anything other than persistent logging, you are almost certainly doing something wrong. Autonomous transactions are a very dangerous and very frequently misused feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文