Oracle trigger error ORA-04091












1















I get an error (ORA-04091: table DBPROJEKT_AKTIENDEPOT.AKTIE is mutating, trigger/function may not see it) when executing my trigger:



CREATE OR REPLACE TRIGGER Aktien_Bilanz_Berechnung
AFTER
INSERT OR UPDATE OF TAGESKURS
OR INSERT OR UPDATE OF WERT_BEIM_EINKAUF
ON AKTIE
FOR EACH ROW
DECLARE
bfr number;
Begin
bfr := :new.TAGESKURS - :new.WERT_BEIM_EINKAUF;
UPDATE AKTIE
SET BILANZ = TAGESKURS - WERT_BEIM_EINKAUF;
IF bfr < -50
THEN
DBMS_OUTPUT.PUT_LINE('ACHTUNG: The value (Nr: '||:new.AKTIEN_NR||') is very low!');
END IF;
END;


I want to check the value "BILANZ" after calculating it, wether it is under -50.
Do you have any idea why this error is thrown?



Thanks for any help!










share|improve this question























  • stackoverflow.com/search?q=%5Boracle%5D+table+is+mutating

    – a_horse_with_no_name
    Jun 21 '18 at 11:06
















1















I get an error (ORA-04091: table DBPROJEKT_AKTIENDEPOT.AKTIE is mutating, trigger/function may not see it) when executing my trigger:



CREATE OR REPLACE TRIGGER Aktien_Bilanz_Berechnung
AFTER
INSERT OR UPDATE OF TAGESKURS
OR INSERT OR UPDATE OF WERT_BEIM_EINKAUF
ON AKTIE
FOR EACH ROW
DECLARE
bfr number;
Begin
bfr := :new.TAGESKURS - :new.WERT_BEIM_EINKAUF;
UPDATE AKTIE
SET BILANZ = TAGESKURS - WERT_BEIM_EINKAUF;
IF bfr < -50
THEN
DBMS_OUTPUT.PUT_LINE('ACHTUNG: The value (Nr: '||:new.AKTIEN_NR||') is very low!');
END IF;
END;


I want to check the value "BILANZ" after calculating it, wether it is under -50.
Do you have any idea why this error is thrown?



Thanks for any help!










share|improve this question























  • stackoverflow.com/search?q=%5Boracle%5D+table+is+mutating

    – a_horse_with_no_name
    Jun 21 '18 at 11:06














1












1








1








I get an error (ORA-04091: table DBPROJEKT_AKTIENDEPOT.AKTIE is mutating, trigger/function may not see it) when executing my trigger:



CREATE OR REPLACE TRIGGER Aktien_Bilanz_Berechnung
AFTER
INSERT OR UPDATE OF TAGESKURS
OR INSERT OR UPDATE OF WERT_BEIM_EINKAUF
ON AKTIE
FOR EACH ROW
DECLARE
bfr number;
Begin
bfr := :new.TAGESKURS - :new.WERT_BEIM_EINKAUF;
UPDATE AKTIE
SET BILANZ = TAGESKURS - WERT_BEIM_EINKAUF;
IF bfr < -50
THEN
DBMS_OUTPUT.PUT_LINE('ACHTUNG: The value (Nr: '||:new.AKTIEN_NR||') is very low!');
END IF;
END;


I want to check the value "BILANZ" after calculating it, wether it is under -50.
Do you have any idea why this error is thrown?



Thanks for any help!










share|improve this question














I get an error (ORA-04091: table DBPROJEKT_AKTIENDEPOT.AKTIE is mutating, trigger/function may not see it) when executing my trigger:



CREATE OR REPLACE TRIGGER Aktien_Bilanz_Berechnung
AFTER
INSERT OR UPDATE OF TAGESKURS
OR INSERT OR UPDATE OF WERT_BEIM_EINKAUF
ON AKTIE
FOR EACH ROW
DECLARE
bfr number;
Begin
bfr := :new.TAGESKURS - :new.WERT_BEIM_EINKAUF;
UPDATE AKTIE
SET BILANZ = TAGESKURS - WERT_BEIM_EINKAUF;
IF bfr < -50
THEN
DBMS_OUTPUT.PUT_LINE('ACHTUNG: The value (Nr: '||:new.AKTIEN_NR||') is very low!');
END IF;
END;


I want to check the value "BILANZ" after calculating it, wether it is under -50.
Do you have any idea why this error is thrown?



Thanks for any help!







sql oracle triggers database-trigger






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 21 '18 at 10:43









LeoBielLeoBiel

134




134













  • stackoverflow.com/search?q=%5Boracle%5D+table+is+mutating

    – a_horse_with_no_name
    Jun 21 '18 at 11:06



















  • stackoverflow.com/search?q=%5Boracle%5D+table+is+mutating

    – a_horse_with_no_name
    Jun 21 '18 at 11:06

















stackoverflow.com/search?q=%5Boracle%5D+table+is+mutating

– a_horse_with_no_name
Jun 21 '18 at 11:06





stackoverflow.com/search?q=%5Boracle%5D+table+is+mutating

– a_horse_with_no_name
Jun 21 '18 at 11:06












2 Answers
2






active

oldest

votes


















1














There are several issues here:




  1. Oracle does not allow you to perform a SELECT/INSERT/UPDATE/DELETE against a table within a row trigger defined on that table or any code called from such a trigger, which is why an error occurred at run time. There are ways to work around this - for example, you can read my answers to this question and this question - but in general you will have to avoid accessing the table on which a row trigger is defined from within the trigger.


  2. The calculation which is being performed in this trigger is what is referred to as business logic and should not be performed in a trigger. Putting logic such as this in a trigger, no matter how convenient it may seem to be, will end up being very confusing to anyone who has to maintain this code because the value of BILANZ is changed where someone who is reading the application code's INSERT or UPDATE statement can't see it. This calculation should be performed in the INSERT or UPDATE statement, not in a trigger. It considered good practice to define a procedure to perform INSERT/UPDATE/DELETE operations on a table so that all such calculations can be captured in one place, instead of being spread out throughout your code base.


  3. Within a BEFORE ROW trigger you can modify the values of the fields in the :NEW row variable to change values before they're written to the database. There are times that this is acceptable, such as when setting columns which track when and by whom a row was last changed, but in general it's considered a bad idea.



Best of luck.






share|improve this answer































    2














    You are modifying the table with the trigger. Use a before update trigger:



    CREATE OR REPLACE TRIGGER Aktien_Bilanz_Berechnung
    BEFORE INSERT OR UPDATE OF TAGESKURS OR INSERT OR UPDATE OF WERT_BEIM_EINKAUF
    ON AKTIE
    FOR EACH ROW
    DECLARE
    v_bfr number;
    BEGIN
    v_bfr := :new.TAGESKURS - :new.WERT_BEIM_EINKAUF;
    :new.BILANZ := v_bfr;
    IF v_bfr < -50 THEN
    Raise_Application_Error(-20456,'ACHTUNG: The value (Nr: '|| :new.AKTIEN_NR || ') is very low!');
    END IF;
    END;





    share|improve this answer





















    • 1





      I replaced dbms_output.put_line with Raise_Application_Error so that the error can be seen from everywhere, even from an application. Got rid of useless dual. +1

      – Barbaros Özhan
      Jun 21 '18 at 11:01








    • 1





      @BarbarosÖzhan . . . Those are fine. For some reason, I'm in the habit of using into when I assign values into columns in triggers in Oracle. Don't know why. It might be an archaic habit.

      – Gordon Linoff
      Jun 21 '18 at 11:36











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50966543%2foracle-trigger-error-ora-04091%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    There are several issues here:




    1. Oracle does not allow you to perform a SELECT/INSERT/UPDATE/DELETE against a table within a row trigger defined on that table or any code called from such a trigger, which is why an error occurred at run time. There are ways to work around this - for example, you can read my answers to this question and this question - but in general you will have to avoid accessing the table on which a row trigger is defined from within the trigger.


    2. The calculation which is being performed in this trigger is what is referred to as business logic and should not be performed in a trigger. Putting logic such as this in a trigger, no matter how convenient it may seem to be, will end up being very confusing to anyone who has to maintain this code because the value of BILANZ is changed where someone who is reading the application code's INSERT or UPDATE statement can't see it. This calculation should be performed in the INSERT or UPDATE statement, not in a trigger. It considered good practice to define a procedure to perform INSERT/UPDATE/DELETE operations on a table so that all such calculations can be captured in one place, instead of being spread out throughout your code base.


    3. Within a BEFORE ROW trigger you can modify the values of the fields in the :NEW row variable to change values before they're written to the database. There are times that this is acceptable, such as when setting columns which track when and by whom a row was last changed, but in general it's considered a bad idea.



    Best of luck.






    share|improve this answer




























      1














      There are several issues here:




      1. Oracle does not allow you to perform a SELECT/INSERT/UPDATE/DELETE against a table within a row trigger defined on that table or any code called from such a trigger, which is why an error occurred at run time. There are ways to work around this - for example, you can read my answers to this question and this question - but in general you will have to avoid accessing the table on which a row trigger is defined from within the trigger.


      2. The calculation which is being performed in this trigger is what is referred to as business logic and should not be performed in a trigger. Putting logic such as this in a trigger, no matter how convenient it may seem to be, will end up being very confusing to anyone who has to maintain this code because the value of BILANZ is changed where someone who is reading the application code's INSERT or UPDATE statement can't see it. This calculation should be performed in the INSERT or UPDATE statement, not in a trigger. It considered good practice to define a procedure to perform INSERT/UPDATE/DELETE operations on a table so that all such calculations can be captured in one place, instead of being spread out throughout your code base.


      3. Within a BEFORE ROW trigger you can modify the values of the fields in the :NEW row variable to change values before they're written to the database. There are times that this is acceptable, such as when setting columns which track when and by whom a row was last changed, but in general it's considered a bad idea.



      Best of luck.






      share|improve this answer


























        1












        1








        1







        There are several issues here:




        1. Oracle does not allow you to perform a SELECT/INSERT/UPDATE/DELETE against a table within a row trigger defined on that table or any code called from such a trigger, which is why an error occurred at run time. There are ways to work around this - for example, you can read my answers to this question and this question - but in general you will have to avoid accessing the table on which a row trigger is defined from within the trigger.


        2. The calculation which is being performed in this trigger is what is referred to as business logic and should not be performed in a trigger. Putting logic such as this in a trigger, no matter how convenient it may seem to be, will end up being very confusing to anyone who has to maintain this code because the value of BILANZ is changed where someone who is reading the application code's INSERT or UPDATE statement can't see it. This calculation should be performed in the INSERT or UPDATE statement, not in a trigger. It considered good practice to define a procedure to perform INSERT/UPDATE/DELETE operations on a table so that all such calculations can be captured in one place, instead of being spread out throughout your code base.


        3. Within a BEFORE ROW trigger you can modify the values of the fields in the :NEW row variable to change values before they're written to the database. There are times that this is acceptable, such as when setting columns which track when and by whom a row was last changed, but in general it's considered a bad idea.



        Best of luck.






        share|improve this answer













        There are several issues here:




        1. Oracle does not allow you to perform a SELECT/INSERT/UPDATE/DELETE against a table within a row trigger defined on that table or any code called from such a trigger, which is why an error occurred at run time. There are ways to work around this - for example, you can read my answers to this question and this question - but in general you will have to avoid accessing the table on which a row trigger is defined from within the trigger.


        2. The calculation which is being performed in this trigger is what is referred to as business logic and should not be performed in a trigger. Putting logic such as this in a trigger, no matter how convenient it may seem to be, will end up being very confusing to anyone who has to maintain this code because the value of BILANZ is changed where someone who is reading the application code's INSERT or UPDATE statement can't see it. This calculation should be performed in the INSERT or UPDATE statement, not in a trigger. It considered good practice to define a procedure to perform INSERT/UPDATE/DELETE operations on a table so that all such calculations can be captured in one place, instead of being spread out throughout your code base.


        3. Within a BEFORE ROW trigger you can modify the values of the fields in the :NEW row variable to change values before they're written to the database. There are times that this is acceptable, such as when setting columns which track when and by whom a row was last changed, but in general it's considered a bad idea.



        Best of luck.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 21 '18 at 11:36









        Bob JarvisBob Jarvis

        33.9k55785




        33.9k55785

























            2














            You are modifying the table with the trigger. Use a before update trigger:



            CREATE OR REPLACE TRIGGER Aktien_Bilanz_Berechnung
            BEFORE INSERT OR UPDATE OF TAGESKURS OR INSERT OR UPDATE OF WERT_BEIM_EINKAUF
            ON AKTIE
            FOR EACH ROW
            DECLARE
            v_bfr number;
            BEGIN
            v_bfr := :new.TAGESKURS - :new.WERT_BEIM_EINKAUF;
            :new.BILANZ := v_bfr;
            IF v_bfr < -50 THEN
            Raise_Application_Error(-20456,'ACHTUNG: The value (Nr: '|| :new.AKTIEN_NR || ') is very low!');
            END IF;
            END;





            share|improve this answer





















            • 1





              I replaced dbms_output.put_line with Raise_Application_Error so that the error can be seen from everywhere, even from an application. Got rid of useless dual. +1

              – Barbaros Özhan
              Jun 21 '18 at 11:01








            • 1





              @BarbarosÖzhan . . . Those are fine. For some reason, I'm in the habit of using into when I assign values into columns in triggers in Oracle. Don't know why. It might be an archaic habit.

              – Gordon Linoff
              Jun 21 '18 at 11:36
















            2














            You are modifying the table with the trigger. Use a before update trigger:



            CREATE OR REPLACE TRIGGER Aktien_Bilanz_Berechnung
            BEFORE INSERT OR UPDATE OF TAGESKURS OR INSERT OR UPDATE OF WERT_BEIM_EINKAUF
            ON AKTIE
            FOR EACH ROW
            DECLARE
            v_bfr number;
            BEGIN
            v_bfr := :new.TAGESKURS - :new.WERT_BEIM_EINKAUF;
            :new.BILANZ := v_bfr;
            IF v_bfr < -50 THEN
            Raise_Application_Error(-20456,'ACHTUNG: The value (Nr: '|| :new.AKTIEN_NR || ') is very low!');
            END IF;
            END;





            share|improve this answer





















            • 1





              I replaced dbms_output.put_line with Raise_Application_Error so that the error can be seen from everywhere, even from an application. Got rid of useless dual. +1

              – Barbaros Özhan
              Jun 21 '18 at 11:01








            • 1





              @BarbarosÖzhan . . . Those are fine. For some reason, I'm in the habit of using into when I assign values into columns in triggers in Oracle. Don't know why. It might be an archaic habit.

              – Gordon Linoff
              Jun 21 '18 at 11:36














            2












            2








            2







            You are modifying the table with the trigger. Use a before update trigger:



            CREATE OR REPLACE TRIGGER Aktien_Bilanz_Berechnung
            BEFORE INSERT OR UPDATE OF TAGESKURS OR INSERT OR UPDATE OF WERT_BEIM_EINKAUF
            ON AKTIE
            FOR EACH ROW
            DECLARE
            v_bfr number;
            BEGIN
            v_bfr := :new.TAGESKURS - :new.WERT_BEIM_EINKAUF;
            :new.BILANZ := v_bfr;
            IF v_bfr < -50 THEN
            Raise_Application_Error(-20456,'ACHTUNG: The value (Nr: '|| :new.AKTIEN_NR || ') is very low!');
            END IF;
            END;





            share|improve this answer















            You are modifying the table with the trigger. Use a before update trigger:



            CREATE OR REPLACE TRIGGER Aktien_Bilanz_Berechnung
            BEFORE INSERT OR UPDATE OF TAGESKURS OR INSERT OR UPDATE OF WERT_BEIM_EINKAUF
            ON AKTIE
            FOR EACH ROW
            DECLARE
            v_bfr number;
            BEGIN
            v_bfr := :new.TAGESKURS - :new.WERT_BEIM_EINKAUF;
            :new.BILANZ := v_bfr;
            IF v_bfr < -50 THEN
            Raise_Application_Error(-20456,'ACHTUNG: The value (Nr: '|| :new.AKTIEN_NR || ') is very low!');
            END IF;
            END;






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 21 '18 at 10:58









            Barbaros Özhan

            12.6k71532




            12.6k71532










            answered Jun 21 '18 at 10:47









            Gordon LinoffGordon Linoff

            764k35296400




            764k35296400








            • 1





              I replaced dbms_output.put_line with Raise_Application_Error so that the error can be seen from everywhere, even from an application. Got rid of useless dual. +1

              – Barbaros Özhan
              Jun 21 '18 at 11:01








            • 1





              @BarbarosÖzhan . . . Those are fine. For some reason, I'm in the habit of using into when I assign values into columns in triggers in Oracle. Don't know why. It might be an archaic habit.

              – Gordon Linoff
              Jun 21 '18 at 11:36














            • 1





              I replaced dbms_output.put_line with Raise_Application_Error so that the error can be seen from everywhere, even from an application. Got rid of useless dual. +1

              – Barbaros Özhan
              Jun 21 '18 at 11:01








            • 1





              @BarbarosÖzhan . . . Those are fine. For some reason, I'm in the habit of using into when I assign values into columns in triggers in Oracle. Don't know why. It might be an archaic habit.

              – Gordon Linoff
              Jun 21 '18 at 11:36








            1




            1





            I replaced dbms_output.put_line with Raise_Application_Error so that the error can be seen from everywhere, even from an application. Got rid of useless dual. +1

            – Barbaros Özhan
            Jun 21 '18 at 11:01







            I replaced dbms_output.put_line with Raise_Application_Error so that the error can be seen from everywhere, even from an application. Got rid of useless dual. +1

            – Barbaros Özhan
            Jun 21 '18 at 11:01






            1




            1





            @BarbarosÖzhan . . . Those are fine. For some reason, I'm in the habit of using into when I assign values into columns in triggers in Oracle. Don't know why. It might be an archaic habit.

            – Gordon Linoff
            Jun 21 '18 at 11:36





            @BarbarosÖzhan . . . Those are fine. For some reason, I'm in the habit of using into when I assign values into columns in triggers in Oracle. Don't know why. It might be an archaic habit.

            – Gordon Linoff
            Jun 21 '18 at 11:36


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50966543%2foracle-trigger-error-ora-04091%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft