Error while running a Trigger

Asked

Viewed 2,766 times

0

I’m having the following error while running my Rigger, how could I resolve?

ERROR

Error when I give Insert in the purchasing table INSERT into compra_product (CPR_CODIGO, PRO_CODIGO,CPP_QTDE, CPP_PRECO,CPP_DESCONTO) VALUES (1,2,7,87,0); ORA-04091: the table ADMIN.COMPRA_PRODUTO is mutant; maybe the trigger/function cannot locate it ORA-06512: in "ADMIN.TRG_TOTAL_COMPRA_PRODUTO", line 3 ORA-04088: error while executing trigger 'ADMIN.TRG_TOTAL_COMPRA_PRODUTO'

TRIGGER

CREATE OR REPLACE TRIGGER "TRG_TOTAL_COMPRA_PRODUTO" 
    BEFORE INSERT OR DELETE OR UPDATE ON COMPRA_PRODUTO

    FOR EACH ROW
DECLARE  

BEGIN

    UPDATE COMPRA_PRODUTO SET CPP_TOTAL = 
        (
            SELECT ((CPP_TOTAL * CPP_QTDE) - (((CPP_TOTAL * CPP_QTDE) * CPP_DESCONTO) / 100)) FROM COMPRA_PRODUTO
                WHERE CPP_CODIGO = :NEW.CPP_CODIGO AND
                        PRO_CODIGO = :NEW.PRO_CODIGO
        ) 
    WHERE CPR_CODIGO = :NEW.CPR_CODIGO;

END;

Insert

INSERT INTO COMPRA_PRODUTO (CPR_CODIGO, PRO_CODIGO, CPP_QTDE, CPP_PRECO, CPP_DESCONTO) 
    VALUES (1,3,9,90, 0);

List of the Database inserir a descrição da imagem aqui

  • 1

    Which doubt??

  • 1

    Da erro quando dou insert na tabela compra_produto para calcular total
INSERT into compra_produto (CPR_CODIGO, PRO_CODIGO,CPP_QTDE, CPP_PRECO,CPP_DESCONTO)
 VALUES (1,2,7,87,0);

ORA-04091: a tabela ADMIN.COMPRA_PRODUTO é mutante; maybe the trigger/function cannot locate it ORA-06512: in "ADMIN.TRG_TOTAL_COMPRA_PRODUTO", line 3 ORA-04088: error while executing 'ADMIN.TRG_TOTAL_COMPRA_PRODUTO'

  • 1

    It seems that the table is not in the same schema. Create a public synonym for the COMPRA_PRODUTO table and try again. Oracle Docs: https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7001.htm

  • there’s only one scheme, I’ve heard about making select on the same table as Rigger, I can’t do it?

1 answer

2

This is the famous case of mutant tables, which are basically triggers that try to query/modify the same table that fires the Trigger, is a standard behavior of oracle.

To solve your problem you will need to handle this calculation directly in the operation of Insert/update or create a procedure that is triggered after performing these operations.

  • I believe that select is unnecessary because the column values are filled by ":new" < :new.CPP_TOTAL = :newCPP_TOTAL * ... > the column CP_TOTAL itself could be virtual calculated https://oracle-base.com/articles/11g/virtual-columns-11gr1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.