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!

ORA-04091: table HR.TABLE_NAME is mutating, trigger/function may not see it

Paul SusantoMar 30 2018 — edited Apr 3 2018

Dear Sir/Madam,

I am using Oracle Database 12C.

I am getting "ORA-04091: table HR.TABLE_NAME is mutating, trigger/function may not see it'. The sample workspace to replicate the issue is shown below:

1. Create transactions table as below:

CREATE TABLE transactions (

transaction\_id   NUMBER(10) PRIMARY KEY,

claim\_id         NUMBER(10),

paid\_status      NUMBER(1)

);

2. Insert Data into transactions table

INSERT INTO transactions values(1,100,1);

INSERT INTO transactions values(2,100,0);

INSERT INTO transactions values(3,100,1);

INSERT INTO transactions values(4,100,1);

INSERT INTO transactions values(5,100,1);

3. Commit the changes

COMMIT;

4. Query the inserted data from transactions table

SELECT * FROM transactions;

pastedImage_15.png

5. Create the check_if_all_paid

CREATE OR REPLACE PROCEDURE check_if_all_paid (

    p\_claim\_id\_i   IN NUMBER

) AS

--PRAGMA autonomous\_transaction;

lv\_unpaid\_count     NUMBER(10);

BEGIN

SELECT COUNT(claim\_id) into lv\_unpaid\_count FROM transactions

    WHERE claim\_id = p\_claim\_id\_i AND (paid\_status != 1);   

END check_if_all_paid;

6. Create the Trigger which will be fired as soon as any update happens in the transaction table

CREATE OR REPLACE TRIGGER trg_test AFTER

UPDATE OF paid\_status ON transactions

FOR EACH ROW

BEGIN

check\_if\_all\_paid(:old.claim\_id);

END;

7. Try to update the transaction table using the below UPDATE statement

UPDATE transactions SET paid_status = 1 WHERE transaction_id = 2;

Thus I am getting the ORA-04091: table HR.TRANSACTIONS is mutating, trigger/function may not see it error as shown below.

pastedImage_39.png

Solution: As a solution for this I tried to put PRAGMA autonomous_transaction inside check_if_all_paid procedure, which solve the problem, but my further requirement which is to get the count of PAID_STATUS inside procedure gets affected giving me wrong count.

Hence I cannot put PRAGMA autonomous_transaction.

I tried many other approach but still failed to solved the requirement.

Thanks & Regards,

Susanto Paul

This post has been answered by Paul Susanto on Mar 30 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 1 2018
Added on Mar 30 2018
17 comments
899 views