Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Some :New values are null in compound trigger

840960Feb 18 2011 — edited Feb 18 2011
<h4>
Hello everybody,

I use Oracle 11g R2.

My problem is: Why certain :New.values are Null in the before statement only if I have an after statement (that do nothing) ?

My test case:
</h4>
create table TEST
(
ID Number,
CODE char(10),
SUBJECT varchar2(60)
)
;
alter table TEST add constraint TEST_PK primary key (CODE);

insert into TEST (CODE, SUBJECT, ID) values ('AAA ', 'Subject AAA', 1);
insert into TEST (CODE, SUBJECT, ID) values ('BBB ', 'Subject BBB', 2);
commit;

<h4>My problem:</h4>
create or replace trigger Test_Trg
for insert or update on Test
compound trigger

before each row is
begin
dbms_output.put_line( 'Before Each Row, New values= Id: '||:New.Id||', Code: '||:New.Code||', Subject: '||:New.Subject );
dbms_output.put_line( 'Before Each Row, Old values= Id: '||:Old.Id||', Code: '||:Old.Code||', Subject: '||:Old.Subject );
:New.Code := Nvl(:New.Code, 'CODE'||:New.Id);
end before each row;

after statement is
begin
dbms_output.put_line( 'After Statement' );
end after statement;

end Test_Trg;
/

<h4>Now if I do that:</h4>
update Test set Subject = 'The New Subject' where ID = 1;
commit;

<h4>The dbms_output is :</h4>
Before Each Row, New values= Id: 1, Code: , Subject:
Before Each Row, Old values= Id: 1, Code: USER1 , Subject: The New Subject
After Statement
<h4> (You can see that in the before each row, :New.Code and :New.Subject are Null!)
And the Data are:</h4>
select * from Test where Id = 1;
Id Code Subject
1 CODE1 The New Subject

<h4>Why the column Code is changed?
Now if I change my trigger (just removing the after statement)</h4>
create or replace trigger Test_Trg
for insert or update on Test
compound trigger

before each row is
begin
dbms_output.put_line( 'Before Each Row, New values= Id: '||:New.Id||', Code: '||:New.Code||', Subject: '||:New.Subject );
dbms_output.put_line( 'Before Each Row, Old values= Id: '||:Old.Id||', Code: '||:Old.Code||', Subject: '||:Old.Subject );

:New.Code := Nvl(:New.Code, 'CODE'||:New.Id);
end before each row;
end Test_Trg;
/
<h4>All work perfect!

My question is: Why certain :New.values are Null in the before statement only if I have an after statement that do nothing ?

Thanks in advance,
</h4>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 18 2011
Added on Feb 18 2011
3 comments
437 views