-1
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
4

5 回答 5

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;
于 2012-12-27T15:51:08.273 回答
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
于 2012-12-27T14:20:14.667 回答
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;  
于 2012-12-27T14:57:12.603 回答
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;
于 2012-12-27T16:23:50.250 回答
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;
于 2012-12-27T14:21:15.900 回答