简体   繁体   中英

Oracle SQL ERROR ORA-04091: table is mutating, trigger/function may not see it

thank you to whoever is reading this.

I made a trigger that is to be used in conjunction with a series of inserts I have made for a certain "Donations" table. The purpose of the trigger is to prevent any invalid entries into the table from showing up in the output of the select query after the entries have finished inserting. I will include a series of screenshots detailing the problem.

Here is the schema containing all the columns of the table:


CREATE TABLE donation (
    don_id              NUMBER PRIMARY KEY
    ,donor_first_name   VARCHAR2(16)
    ,donor_last_name    VARCHAR2(16)
    ,donation_date      DATE NOT NULL
    ,donation_amount    NUMBER(7,1) NOT NULL
    ,type_of_donation   VARCHAR2(12)
    ,address_id         NUMBER NOT NULL
    ,volunteer_id       NUMBER NOT NULL
    ,CONSTRAINT fk_don_add FOREIGN KEY ( address_id )
        REFERENCES address ( address_id )
    ,CONSTRAINT fk_don_vol FOREIGN KEY ( volunteer_id )
        REFERENCES volunteer ( volunteer_id )
    );

Here are the inserts. There are 60 entries in total, and a few of them are invalid (by containing empty entries in the name columns etc.). I cut down the insert command into two photos that show the beginning and end:


INSERT ALL 
        INTO donation VALUES(001, 'Michael', 'Travis', '2021-03-16', 20, 'Cash', 39854, 100)
        INTO donation VALUES(002, 'Mitaeo', 'Akkaboune', '2021-03-22', 40, 'Cash', 7021, 141)
        INTO donation VALUES(003, 'Paul', 'Friat', '2021-03-22', 20, 'Cash', 7232, 141)
        ...
        INTO donation VALUES(059, 'Sasha', 'Wernicke', '2021-01-30', 30, 'Cash', 18991, 247)
        INTO donation VALUES(060, 'Samantha', 'Weiner', '2021-02-26', 60, 'Cheque', 30695, 146)
SELECT * FROM DUAL;

This is the trigger I created:


    CREATE OR REPLACE TRIGGER IgnoreFaultyDonors
    BEFORE INSERT ON donation
    FOR EACH ROW
    BEGIN
        INSERT INTO donation (don_id, donor_first_name, donor_last_name, 
        donation_date, donation_amount, type_of_donation, address_id, 
        volunteer_id)
            SELECT don_id, donor_first_name, donor_last_name, donation_date, 
                    donation_amount, type_of_donation, address_id, volunteer_id
            FROM donation
            WHERE donor_first_name IS NOT NULL 
            AND donor_last_name IS NOT NULL
            AND (type_of_donation = 'Cash' OR type_of_donation = 'Cheque');
    END;
    /

And this is the error I keep getting:

So yeah. I am wondering if there is an easy way to change this trigger I have here so that I can run the insert statements and subsequent select query without having triggering these kinds of errors. The inserts work just fine when the trigger is not enabled, but I need the invalid entries in the table to be withheld in my final output. I looked up compound triggers as a lot of solutions I have read so far from searching online mentioned to use them, but I'm coming across so many varied examples that it's hard for me to form a starting strategy in using them.

Any constructive advice and hints are much appreciated. In the meantime, I am going to keep researching.

PS Sorry if the images aren't good in case you wanted to copy/paste and test my code. I wanted to implement my code directly in here but the Code Sample feature isn't working very well. This is only my second post here so I'm quite inexperienced with this site.

You try to select data inside row level trigger from the same table at witch inserting this trigger is raised. That's why you got such error.

You don't need additional insert in the trigger on the same table. Just insert certain rows witch fits your condition.

But if you are going to make much inserting in a future you can do next:

  1. Create view as select * from yourTable
  2. Create trigger instead of insert on yourView
  3. Add conditional inserting in yourTable in this trigger
  4. Make inserting in this view instead of table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM