plsql learning - six:DML trigger

The trigger triggered when DML sentences executed,the steps are:

1)If there are before-triggers before the sentence,run these triggers.

2)For every row of the sentences that involved:

   a.if there are before-triggers for each row,run these trigger.

   b.execute this sentence

   c.if there are after--triggers for each row,run these trigger.

3)If there are after-triggers after the sentence,run these triggers.

Here is the example:

create sequence trig_seq start with 1 increment by 1;

create or replace package TrigPackage as
  v_Counter number;
end TrigPackage;

create or replace trigger ClassesBStatement
  before update on classes
begin
  TrigPackage.v_Counter := 0;
  insert into temp_table(num_col,char_col)
  values(trig_seq.nextval,'Before Statement: counter = '||TrigPackage.v_Counter);
  --increment for the next trigger
  TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
end ClassesBStatement;

create or replace trigger ClassesAStatement1
  after update on classes
begin
  --TrigPackage.v_Counter := 0;
  insert into temp_table(num_col,char_col)
  values(trig_seq.nextval,'After Statement1: counter = '||TrigPackage.v_Counter);
  TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
end ClassesAStatement1;

create or replace trigger ClassesAStatement2
  after update on classes
begin
  --TrigPackage.v_Counter := 0;
  insert into temp_table(num_col,char_col)
  values(trig_seq.nextval,'After Statement2: counter = '||TrigPackage.v_Counter);
  TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
end ClassesAStatement2;

create or replace trigger ClassesBRow1
  before update on classes
  for each row
begin
  --TrigPackage.v_Counter := 0;
  insert into temp_table(num_col,char_col)
  values(trig_seq.nextval,'Before Row1: counter = '||TrigPackage.v_Counter);
  TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
end ClassesBRow1;

create or replace trigger ClassesBRow2
  before update on classes
  for each row
begin
  --TrigPackage.v_Counter := 0;
  insert into temp_table(num_col,char_col)
  values(trig_seq.nextval,'Before Row2: counter = '||TrigPackage.v_Counter);
  TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
end ClassesBRow2;

create or replace trigger ClassesBRow3
  before update on classes
  for each row
begin
  --TrigPackage.v_Counter := 0;
  insert into temp_table(num_col,char_col)
  values(trig_seq.nextval,'Before Row3: counter = '||TrigPackage.v_Counter);
  TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
end ClassesBRow3;

create or replace trigger ClassesARow
  after update on classes
  for each row
begin
  --TrigPackage.v_Counter := 0;
  insert into temp_table(num_col,char_col)
  values(trig_seq.nextval,'After Row: counter = '||TrigPackage.v_Counter);
  TrigPackage.v_Counter := TrigPackage.v_Counter + 1;
end ClassesARow;



 

then test it by followed code:

update classes c set c.num_credits = 4 where c.department in ('HIS','CS')
select * from temp_table order by num_col

 

the result is:

  1 Before Statement: counter = 0
2 Before Row3: counter = 1
3 Before Row2: counter = 2
4 Before Row1: counter = 3
5 After Row: counter = 4
6 Before Row3: counter = 5
7 Before Row2: counter = 6
8 Before Row1: counter = 7
9 After Row: counter = 8
10 Before Row3: counter = 9
11 Before Row2: counter = 10
12 Before Row1: counter = 11
13 After Row: counter = 12
14 Before Row3: counter = 13
15 Before Row2: counter = 14
16 Before Row1: counter = 15
17 After Row: counter = 16
18 After Statement2: counter = 17
19 After Statement1: counter = 18

你可能感兴趣的:(C++,c,C#)