I need help solving a problem, I tried halfway, but it is not working.
I have two tables which I have to perform this activity and create a trigger
called trg_line_total
to write the line_total
value in the LINE
table whenever a new row is added in LINE
.
The value of LINE_TOTAL
is the product of the values of LINE_UNITS
(Line Product Unit) and LINE_PRICE
(Line Product Price)
CREATE TABLE INVOICE(
INV_NUMBER number primary key,
INV_DATE date default SYSDATE not null,
INV_SUBTOTAL NUMBER(9,2)DEFAULT 0.00,
INV_TAX NUMBER(9,2)DEFAULT 0.00,
INV_TOTAL NUMBER(9,2)DEFAULT 0.00,
CONSTRAINT INV_CK1 CHECK(INV_DATE>TO_DATE('01-JAN-2008','DD-MON-YYYY')));
CREATE TABLE LINE(
INV_NUMBER number not null,
LINE_NUMBER number not null,
LINE_UNITS Number default 0 not null,
LINE_PRICE number(9,2) default 0.00 not null,
LINE_TOTALE number(9,2) default 0.00 not null,
primary key(INV_NUMBER,LINE_NUMBER),
foreign key(INV_NUMBER)references INVOICE on delete cascade);
CREATE OR REPLACE TRIGGER trg_line_total
AFTER INSERT ON line
FOR EACH ROW
DECLARE
total line.line_totale%TYPE;
codigo line.inv_number%TYPE;
BEGIN
SELECT inv_number
INTO codigo
FROM invoice
WHERE inv_number = :new.inv_number;
total := :new.line_units * :new.line_price;
UPDATE line
SET line_totale = line_totale + total
WHERE inv_number = codigo;
END;