(GO) Oracle triggers use: simple use of after insert with before insert note

Source: Internet
Author: User

This article was reproduced from: http://blog.csdn.net/kuangfengbuyi/article/details/41446125

When you create a trigger, the trigger type is after insert,

In the Begin

Select Fielda to v_a from TableA;

When executing to this sentence, an error occurs:

--pop-up error message hint

--ora-04091: Table tr_table has changed trigger/function cannot read it

--ORA-06512: at Iu_table Line 2

--ora-04088: Trigger iu_table Error during execution

Problem Analysis:

When executing DML statements in Oracle, it is necessary to display the commit operation. When we insert, triggers are triggered to perform various actions on the trigger table and the extension table, but at this point the trigger and the insert are in the same transaction management, so we cannot do any additional action on the trigger table if the INSERT statement is not committed. If other additional actions are performed, the exception information is thrown as above.

Solution:

The reason for the error is that the trigger and DML statements are in the same transaction management, so scenario one is to divide the triggers and DML statements into two separate transactions. Pragma autonomous_transaction can be used here; Tells the Oracle trigger that a custom transaction is being processed.

Declare

pragma autonomous_transaction;

Begin

-------

--you need to show the commit transaction here

Commit;

End;

At the same time, the trigger cannot update the row where the trigger condition resides.

For example:

Create or replace Trigger tri_a

After insert on TableA

For each row

Declare

pragma autonomous_transaction;

Begin

Update TableA set fielda=1 where id=:new.id;

End

When you insert the data, you will find that the UPDATE statement is executed but does not take effect.

Then want to change: new.fielda= 1, after execution, error:

ORA-04084: Cannot change the new value of this trigger type

At this time, to change the strategy, but also according to individual needs,

Just what I need. You can update the trigger type after insert to before insert

(GO) Oracle triggers use: simple use of after insert with before insert note

Related Article

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.