Example of before and after triggers in Oracle

Source: Internet
Author: User

Make a table insert trigger to modify only the fields of the inserted rows.

Create or replace trigger tr_rme_slot
Before insert on rme_slot
For each row
Begin
If (: New. Position> = 0 and: New. Position <10) then
: New. slot_name: = '0' | to_char (: New. position );
Else
: New. slot_name: = to_char (: New. position );
End if;
End;

The insert row needs to be modified before insertion. The update statement is not required in trigger implementation.

At the same time, if you want to modify the table records in the trigger, you need to write as follows:

Create or replace trigger tr_rme_slot
After insert on rme_slot
For each row
Declare
Pragma autonomous_transaction;
Begin
If (: New. Position> = 0 and: New. Position <10) then
Update rme_slot set slot_name = '0' | to_char (: New. Position) Where slot_id =: New. slot_id;
Else
Update rme_slot set slot_name = to_char (: New. Position) Where slot_id =: New. slot_id;
End if;
Commit;
End;

Note that there is an additional declare section, and commit is required when the trigger ends.

Create or replace trigger tr_afterupdate
After insert or update
On test_trigger
For each row
Declare
Pragma autonomous_transaction;
Begin

Update test_trigger
Set t_fact = t_salary * (1-t_tax)-T_EAT-T_HOUSE
Where t_name =: New. t_name;
Commit;

Exception
When others then
-- Consider logging the error and then re-raise
Raise;
End tr_testupdate;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.