简体   繁体   中英

how many times the row level trigger will fire when we update 10 rows

How many times the **row level trigger**  will fire  when we update 10 rows?

假设一个表中有10行带有行级触发器,那么当我们更新10行时,行级触发器将触发多少次?

In most cases if you update 10 rows trigger will be fired 10 times. But there are cases what violate this generic rule and wich are nicely described by Tom Kyte. These cases are "DML statement restart".

http://tkyte.blogspot.ru/2010/04/that-old-restart-problem-again.html

Please read carefuly. I don't say anything brand new, just in short:

1) Suppose I create row-level before update trigger for the table:

SQL> create table t (x int, y int);

Table created.

SQL> insert into t values(1,1);

1 row inserted.

SQL> commit;


SQL> create or replace trigger t_tr
  2  before update on t
  3  for each row
  4  begin
  5    dbms_output.put_line('Fired');
  6  end;
  7  /

Trigger created.

Now if I update table in the single session - I get "1 row updated = 1 time trigger fired":

SQL> set serveroutput on
SQL> update t set x = 1;
Fired                                                                           

1 row updated.


SQL> rollback;

Suppose now in one session I update table T and change value of Y column:

SQL> update t set y = 2 where y = 1;

1 row updated.

In second session I'm trying to update column X in table T and rely on value Y=1 wich is being changed in the first transaction:

SQL> update t set x = 1 where y = 1;

I have to wait for the first transaction completion. After commit in the first transaction I get in the second one:

SQL> update t set x = 1 where y = 1;
Fired                                                                           

0 rows updated.

As you can see the trigger was fires once but the statement updated 0 rows. This is because Oracle had to restart DML statement when it found the current row it's trying to update is not in consistent state (after first transaction update) with WHERE clause condition.

Triggers what refer to :old values can cause DML restart too. The interesting examples you can find in Tom Kyte blog or his famost book. So you should not rely on the rule "N rows changed - N times trigger fired" in general.

根据代码中的触发时间,它将触发10次,每行一次,由于更新原因而受影响,每行触发一次

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM