2
I need help to solve a problem, I tried it halfway, but it’s not working.
I have two tables to which I have to perform this activity and create a trigger called trg_line_total to save the value of line_total on the table LINE whenever a new line is added in LINE.
The value of LINE_TOTAL is the product of the values of LINE_UNITS(Product unit of the line) and LINE_PRICE(Price of the product of the line)
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;
Research on
trigger mutantthat will help you because you are trying to change a record of the table itself that is being included.– David
Maybe a STATEMENT rigger and using Constint DEFERRABLE
– Motta