create or replace TRIGGER TRG_DecreaseQuantity
AFTER INSERT
ON V_SALE FOR EACH ROW
BEGIN
UPDATE VEHICLE
SET V.V_QUANTITY=(SELECT CASE
WHEN V.V_QUANTITY >= S.QUANTITY AND
V.VEHICLE_ID = S.VEHICLE_ID_FK
THEN V.V_QUANTITY = V.V_QUANTITY-S.QUANTITY
WHEN V.V_QUANTITY < S.QUANTITY
THEN V.V_QUANTITY = S.QUANTITY
FROM VEHICLE V,
V_SALE S
WHERE V.VEHICLE_ID=S.VEHICLE_ID_FK
)
WHERE V.VEHICLE_ID = :NEW.VEHICLE_ID_FK;
END;
我们有一个autoGallery
数据库,其中有表格。
必须控制两个表V_SALE
和VEHICLE
。当我出售车辆时,我想控制车辆数量,然后减少数量。
Vehicle(
Vehicle_ID Primary key,
V_QUANTITY
...
)
V_SALE(
VEHICLE_ID_FK FOREIGN KEY,
QUANTITY
...
)
我收到了以下错误:
Compilation failed, line 5 (16:03:48) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers.
PL/SQL: ORA-00905: missing keyword
Compilation failed, line 2 (16:03:48) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers.
PL/SQL: SQL Statement ignored
答案 0 :(得分:1)
正确的CASE语句必须以END结尾。并且你不能在THEN条款中有一个等号,既不作为比较也不作为分配。
CASE
WHEN V.V_QUANTITY >= S.QUANTITY AND V.VEHICLE_ID = S.VEHICLE_ID_FK
THEN V.V_QUANTITY - S.QUANTITY
WHEN V.V_QUANTITY < S.QUANTITY
THEN S.QUANTITY
END
答案 1 :(得分:1)
尝试给出这样的别名:
UPDATE VEHICLE V
SET V.V_QUANTITY=CASE WHEN V.V_QUANTITY >= :NEW.QUANTITY
THEN V.V_QUANTITY-:NEW.QUANTITY
WHEN V.V_QUANTITY < :NEW.QUANTITY
THEN :NEW.QUANTITY
END
WHERE V.VEHICLE_ID = :NEW.VEHICLE_ID_FK;
答案 2 :(得分:0)
试试这个,'CASE WHEN...END'
语法错误
create or replace TRIGGER TRG_DecreaseQuantity
AFTER INSERT
ON V_SALE FOR EACH ROW
BEGIN
UPDATE VEHICLE
SET V.V_QUANTITY=(SELECT
CASE
WHEN V.V_QUANTITY >=S.QUANTITY AND V.VEHICLE_ID=S.VEHICLE_ID_FK THEN V.V_QUANTITY-S.QUANTITY
WHEN V.V_QUANTITY<S.QUANTITY THEN S.QUANTITY END
FROM VEHICLE V,V_SALE S
WHERE V.VEHICLE_ID=S.VEHICLE_ID_FK)
WHERE V.VEHICLE_ID=:NEW.VEHICLE_ID_FK;
END;
正如您要求IF ELSE示例我正在添加此示例示例并根据您的需要应用
CREATE OR REPLACE TRIGGER "TRG_PAYMONEY"
AFTER INSERT
ON PAYMENTDEVICE FOR EACH ROW
BEGIN
IF :NEW.MEALTYPE='GUEST' THEN
UPDATE CARD
SET CREDIT = CREDIT - 5
WHERE CARDID = :NEW.CHARGEDCARDID;
ELSE
UPDATE CARD
SET CREDIT = CREDIT - CHARGE
WHERE CARDID = :NEW.CHARGEDCARDID;
END IF;
END;
答案 3 :(得分:0)
你不应该只需要这个吗?
UPDATE VEHICLE V
SET V.V_QUANTITY=CASE WHEN V.V_QUANTITY >= :NEW.QUANTITY
THEN V.V_QUANTITY-:NEW.QUANTITY
WHEN V.V_QUANTITY < :NEW.QUANTITY
THEN :NEW.QUANTITY
END
WHERE V.VEHICLE_ID = :NEW.VEHICLE_ID_FK;
答案 4 :(得分:0)
create or replace TRIGGER "TRG_DecreaseQuantity"
AFTER INSERT
ON V_SALE FOR EACH ROW
BEGIN
UPDATE VEHICLE V
SET V.V_QUANTITY=CASE WHEN V.V_QUANTITY >= :NEW.QUANTITY
THEN V.V_QUANTITY-:NEW.QUANTITY
WHEN V.V_QUANTITY < :NEW.QUANTITY
THEN V.V_QUANTITY
END
WHERE V.VEHICLE_ID = :NEW.VEHICLE_ID_FK;
END;