我正在尝试创建一个更新PRODUCTOS表中某个产品库存的触发器,当在COMPRAS上插入新购买时触发此触发器
CREATE OR REPLACE TRIGGER TRIGGER_COMPRAS
AFTER INSERT ON DCOMPRAS
FOR EACH ROW
DECLARE
NEWSTOCK NUMBER;
BEGIN
SELECT STOCK
INTO NEWSTOCK
FROM PRODUCTOS
WHERE ID_PRODUCTO = :NEW.ID_PRODUCTO;
NEWSTOCK := NEWSTOCK + :NEW.CANTIDAD;
UPDATE INTO PRODUCTOS
(STOCK)
VALUES
(NEWSTOCK)
WHERE ID_PRODUCTO = :NEW.ID_PRODUCTO;
END TRIGGER_COMPRAS;
但是当我尝试运行它时,它会给我:
ERROR at line 9: PLS-00103: Encountered the symbol "=" when expecting one of the following:
:= . ( @ % ;
7. SELECT STOCK
8. INTO NEWSTOCK
9. FROM PRODUCTOS
10. WHERE ID_PRODUCTO = :NEW.ID_PRODUCTO;
11. NEWSTOCK := NEWSTOCK + :NEW.CANTIDAD;
------------ ----------- EDIT
更改了NEWSTOCK:= NEWSTOCK +:NEW.CANTIDAD; 但是,另一个错误:
ERROR at line 11: PL/SQL: ORA-00903: invalid table name
1. CREATE OR REPLACE TRIGGER TRIGGER_COMPRAS
2. AFTER INSERT ON DCOMPRAS
3. FOR EACH ROW
答案 0 :(得分:0)
最终陈述必须是这样的
CREATE OR REPLACE TRIGGER TRIGGER_COMPRAS
AFTER INSERT ON DCOMPRAS REFERENCING OLD AS OLDREC NEW AS NEWREC FOR EACH ROW
DECLARE
NEWSTOCK NUMBER;
BEGIN
SELECT STOCK
INTO NEWSTOCK
FROM PRODUCTOS
WHERE ID_PRODUCTO = :NEW.ID_PRODUCTO;
NEWSTOCK := NEWSTOCK + :NEW.CANTIDAD;
UPDATE PRODUCTOS
SET STOCK = NEWSTOCK
WHERE ID_PRODUCTO = :NEW.ID_PRODUCTO;
END TRIGGER_COMPRAS;