I have created a trigger
that triggers when I modify the value of a record of a table, in this case the number of components that I have in the company. That is, the trigger
only allows me to modify this amount if the new value is less than 999. If not, an exception jumps. The fact is that when the value I enter in the field with a update
is less than 999, I want the trigger
to also change the value of the column amount (which is the product of cantidad*precio_venda
). The case is that whatever I do oracle
returns me the error that the table is mutating and I do not know how I can make the trigger
update the field when I update the amount. To be clear I copy below the code of the table I have created and my trigger
, I think what I have to do is an autonomous transaction but I am not able to perform the correct praxis, I appreciate any help, thanks in advance :) .
Table detail that triggers the trigger
CREATE TABLE DETALLE (
COM_NUM NUMBER (4),
DETALLE_NUM NUMBER (4),
PROD_NUM NUMBER (6) CONSTRAINT DETALLE_NN_PRODUCTO NOT NULL,
PRECIO_VENDA NUMBER (8,2),
CANTIDAD NUMBER (8),
IMPORTE NUMBER (8,2),
CONSTRAINT DETALLE_FK_COMANDA FOREIGN KEY (COM_NUM) REFERENCES COMANDA (COM_NUM),
CONSTRAINT DETALLE_FK_PRODUCTO FOREIGN KEY (PROD_NUM) REFERENCES PRODUCTO (PROD_NUM),
CONSTRAINT DETALLE_PK PRIMARY KEY (COM_NUM,DETALLE_NUM),
CONSTRAINT DETALLE_CK_DETALLE_NUM_POSITIU CHECK (DETALLE_NUM > 0),
CONSTRAINT DETALLE_CK_PVP_NO_NEGATIU CHECK (PRECIO_VENDA >= 0));
Then my trigger
CREATE OR REPLACE TRIGGER verifica_unidades
AFTER INSERT OR UPDATE OF CANTIDAD ON DETALLE
for each row
BEGIN
IF :new.cantidad>999 then
RAISE_APPLICATION_ERROR(-20511,'¡Cantidad demasiado alta!');
end if;
end;
/
Greetings.