我正在尝试设置触发器以在新发票上插入payment_date。这是我原来的触发器,我认为它可以正常工作,但是收到了变异表错误。
CREATE OR REPLACE TRIGGER payment_before_insert_invoice
AFTER INSERT OR UPDATE
ON invoices
FOR EACH ROW
BEGIN
UPDATE invoices set payment_date = get_paymentdate(invoice_date);
END;
get_paymentdate函数只返回基于invoice_date + term的payment_date。
我读到了通过使用复合触发器来解决变异表问题但是没有完全理解它....这是我所拥有但我收到错误。有人能帮忙吗?我在理论上理解,它应该使用数组将数据放入临时表中,但有点丢失,如下所示......
CREATE OR REPLACE TRIGGER payment_before_insert_invoice
FOR UPDATE OF payment_date
ON invoices
COMPOUND TRIGGER
TYPE invoice_date_table IS TABLE OF DATE;
TYPE payment_date_table IS TABLE OF DATE;
TYPE ids_totals_table INDEX BY BINARY_INTEGER;
invoice_dates invoice_ids_table;
payment_dates payment_date_table;
ids_totals ids_totals_table;
BEFORE STATEMENT IS
invoice_date DATE
payment_date DATE;
BEGIN
SELECT invoice_id, payment_date
BULK COLLECT INTO invoice_dates, payment_dates
FROM invoices;
FOR i IN 1...invoice_dates.COUNT() LOOP
invoice_date := invoice_dates(i);
payment_date := payment_dates(i);
ids_totals(invoice_date) := payment_dates;
END LOOP;
END BEFORE STATEMENT;
AFTER EACH ROW IS
payment_date DATE;
BEGIN
UPDATE invoices set payment_date = get_paymentdate(invoice_date);
END;
答案 0 :(得分:0)
嗯,原始触发器中有几个问题。首先,您的UPDATE语句正在尝试更改INVOICES表中的每一行。你对它没有任何限制。即使这样它也行不通。如果您只想计算其中一个字段的值,请将其更改为BEFORE触发器,并使用:new伪参考将函数值分配给感兴趣的列:
CREATE OR REPLACE TRIGGER payment_before_insert_invoice
BEFORE INSERT OR UPDATE
ON invoices
FOR EACH ROW
BEGIN
:new.payment_date = get_paymentdate(:new.invoice_date);
END;