0
My teacher passed that question:
Write a Rigger that checks, during registration/change of a sale, the following:
1) The QUANTITY field of the ITEMVENDA table shall not have decimal places and shall be greater than or equal to 1;
2) The VALUE field of the ITEMVENDA table must be greater than 0.
When the above conditions are not met, the transaction must be cancelled and one of the following messages error should be displayed respectively:
1) "The quantity of the product shall not be fractionated and shall be greater than or equal to 1";
2) "The value of the product shall be greater than 0".
So I made the code:
CREATE OR replace TRIGGER especificacoes
BEFORE INSERT OR UPDATE ON itemvenda
FOR EACH ROW
WHEN (new.quantidade <> round(new.quantidade) OR (new.quantidade < 1) OR (new.valor < 1))
BEGIN
IF (:new.quantidade <> round(:new.quantidade)) OR (:new.quantidade < 1)
THEN
RAISE_APPLICATION_ERROR(-20010,'“A quantidade do produto não pode ser fracionada e deve ser maior ou igual a 1”; ');
END IF;
IF (:new.valor < 1 )
THEN
RAISE_APPLICATION_ERROR(-20022,'“O valor do produto deve ser maior que 0”.');
END IF;
END;
SQL Developer compiled normally and ran some tests (UPDATE AND INSERT), the conditions related to the quantity column are not working. Theoretically, it should appear: "The quantity of the product cannot be fractionated and must be greater than or equal to 1" and block the operation.
This is the error that appears when you have a wrong operation in the value column:
"UPDATE "SYSTEM"." ITEMVENDA" SET VALUE = '0' WHERE ROWID = 'Aaasnqaabaaaagxaae' AND ORA_ROWSCN = '2140564' ORA-20022: "The value of the product must be greater than 0". ORA-06512: in "SYSTEM.SPECIFICATIONS", line 9 ORA-04088: error while executing 'SYSTEM.SPECIFICATIONS''
An error occurred while saving changes to the "SYSTEM"." ITEMVENDA" table: Line 5: ORA-20022: "Product value must be greater than 0". ORA-06512: in "SYSTEM.SPECIFICATIONS", line 9 ORA-04088: error while executing 'SYSTEM.SPECIFICATIONS''"
I am unable to find the error, will this round is breaking the condition of the column quantity?.
I did not understand , was made an update or Insert for the quantity ?
– Motta
I did and he accepted normally.
– Lucas Martins
Theoretically, it should appear: "The quantity of the product cannot be fractionated and must be greater than or equal to 1" and block the operation.
– Lucas Martins
Edits the post and publishes.
– Motta