简体   繁体   中英

Oracle APEX - AFTER INSERT, UPDATE another table trigger

I'm trying to insert into the poPayment table with some of the details from the purchaseOrder table AFTER INSERT ON purchaseorder, to start with I want the

purchaseorder.purchaseorderid added into the popayment.purchaseorderid

after I get this working I want to expand the trigger

here is the purchase order table;

CREATE TABLE purchaseOrder
(
    purchaseOrderid NUMBER (6) NOT NULL,
    staffid NUMBER (6),
    rawMaterialid2 NUMBER (6) NOT NULL,
    supplierContactid2 NUMBER (6),
    orderDate DATE NOT NULL,
    dueDate DATE,
    pricePerUnit NUMBER (7,2),
    qty NUMBER,
    total NUMBER (7,2),

    CONSTRAINT purchaseOrderid PRIMARY KEY(purchaseOrderid)             
);

here is the purchase order payment table;

CREATE TABLE poPayment
(
    poPaymentid NUMBER (6) NOT NULL,
    purchaseOrderid NUMBER (6) NOT NULL,
    staffid NUMBER (6) NOT NULL,
    paymentDate DATE,
    amountPaid NUMBER (7,2),
    qtyOrdered NUMBER (6),
    qtyDelievered NUMBER (6),
    invoiceAmount NUMBER (6),
    dateDelivered DATE,

    CONSTRAINT pk_poPaymentid PRIMARY KEY(poPaymentid)          
);

and here is the trigger I tried

CREATE OR REPLACE TRIGGER createPOpayment_trg

ON purchaseorder

AFTER INSERT
AS BEGIN

INSERT INTO popayment (purchaseorderid)
    SELECT 
    purchaseorder.purchaseorderid
    FROM purchaseorder
    WHERE purchaseorder.purchaseorderid = popayment.purchaseorderid

    END

Thank Brian

You have incorrect syntax for the create trigger statement ; the AFTER INSERT' needs to be before the ON purchaseorder` part (the dml_event_clause in the syntax diagrams). You also want it to be a row-level trigger, so it fires for each individual row you insert in the table;

CREATE OR REPLACE TRIGGER createPOpayment_trg
AFTER INSERT
ON purchaseorder
FOR EACH ROW
AS
...

But then your current insert is malformed too. It's going to insert a row into popayment for every row in purchaseorder that already has a matching popayment row - which is going to duplicate all existing rows, not create one for your new purchase order.

You only want to insert a single row matching the inserted purchase order. And you need to include at least all the not null columns:

BEGIN
  INSERT INTO popayment (poPaymentid, purchaseorderid, staffid)
  VALUES (popayment_seq.nextval, :new.purchaseorderid, :new.staffid);
END;

I'm guessing (and hoping) you have a sequence to populate the poPaymentid primary key. The other two values are coming from the inserted purchaseorder record, using the :new pseudorecord. You do not want to query the table you are inserting into.

If the key is being set by its own trigger then you only need to give the other two not-null columns (plus any more you add later):

BEGIN
  INSERT INTO popayment (purchaseorderid, staffid)
  VALUES (:new.purchaseorderid, :new.staffid);
END;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM