我的任务是必须在插入后创建触发器。以下是涉及的表格:
CREATE TABLE purchase(
orderid CHAR(3),
unit_price NUMBER(7,2),
sales_price NUMBER(7,2) DEFAULT NULL,
itemid CHAR(3)
);
这是触发器:
/* Now we need the AFTER trigger */
CREATE OR REPLACE TRIGGER DoubleSalesPrice
AFTER INSERT on purchase
FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
itemcount smallint;
BEGIN
SELECT COUNT(itemid) INTO itemcount FROM purchase P WHERE P.itemid = :new.itemid;
IF (itemcount = 1) THEN
UPDATE purchase P
SET P.sales_price = 2*P.unit_price
WHERE P.orderid = :new.orderid;
end if;
end;
/
我已将其指定为每个代码的 AFTER INSERT。但是,当我插入购买时,我没有得到想要的结果:
INSERT INTO purchase VALUES( '100','250', '200', '500');
SQL> select * from purchase;
ORD UNIT_PRICE SALES_PRICE ITE
--- ---------- ----------- ---
100 250 200 500
触发器似乎没有触发。我已确保以相同的结果启用它:
ALTER TRIGGER DoubleSalesPrice enable;
SQL> select * from purchase;
ORD UNIT_PRICE SALES_PRICE ITE
--- ---------- ----------- ---
101 250 200 500
如果有人有任何建议,请告诉我。我通常喜欢在尝试实际问题之前构建小示例。这反映了我需要获得的功能。