我正在尝试为更新客户余额的类创建触发器,具体取决于项目返回的早期或早期。我有一个具有截止日期和返回日期的章程表,触发器设计为仅在更新返回日期时触发。然后触发器获取返回日期和截止日期之间的差异,并将该值存储到变量中。我有一系列if else语句,用于确定项目是迟到还是提前返回,然后将天数乘以延迟费用或提前奖金。然后,它将客户表中的客户余额更新为费用变量的值。甲骨文说我的结束语中有语法错误,我不确定是什么问题。
CREATE OR REPLACE TRIGGER Fee_Trigger
AFTER UPDATE ON CHARTER
FOR EACH ROW
WHEN ((:NEW.Return_Date <> :OLD.Return_Date AND :NEW.Return_Date IS NOT
NULL))
DECLARE
Fee NUMBER;
BEGIN
Fee := (:NEW.Return_Date - Due_Date);
IF Fee > 0 THEN Fee := (Fee * 75) ;
ELSE IF Fee < 0 THEN Fee := (Fee * 25);
ELSE IF Fee = 0 THEN FEE := Fee;
END IF;
UPDATE CUSTOMER
SET Customer_Balance = Fee
WHERE CustomerID = :NEW.CustomerID
END;
答案 0 :(得分:1)
有一些小格式错误。以下可以使用:
CREATE OR REPLACE TRIGGER Fee_Trigger
AFTER UPDATE ON CHARTER
FOR EACH ROW
WHEN ((NEW.Return_Date <> OLD.Return_Date AND NEW.Return_Date IS NOT NULL))
DECLARE
Fee NUMBER;
BEGIN
Fee := :NEW.Return_Date - :NEW.Due_Date;
IF Fee > 0 THEN Fee := (Fee * 75);
ELSIF Fee < 0 THEN Fee := (Fee * 25);
ELSIF Fee = 0 THEN Fee := Fee;
END IF;
UPDATE CUSTOMER
SET Customer_Balance = Fee
WHERE CustomerID = :NEW.CustomerID;
END;
遇到以下问题:
:OLD.Due_Date
或:NEW.Due_Date
)ELSIF
代替ELSE IF
UPDATE
语句应以semicolon
结束。 colons
之前的OLD
和NEW
语句中的WHEN
。答案 1 :(得分:0)
我建议在UPDATE语句中用CASE表达式替换IF/ELSIF
语句:
CREATE OR REPLACE TRIGGER Fee_Trigger
AFTER UPDATE ON CHARTER
FOR EACH ROW
WHEN ((:NEW.Return_Date <> :OLD.Return_Date AND :NEW.Return_Date IS NOT NULL))
DECLARE
Due_Date DATE := TRUNC(SYSDATE); -- initialization as an example value.
nDays_between_return_and_due NUMBER;
BEGIN
nDays_between_return_and_due := TRUNC(:NEW.Return_Date - Due_Date);
UPDATE CUSTOMER
SET Customer_Balance = CASE
WHEN nDays_between_return_and_due > 0 THEN -- Returned late
nDays_between_return_and_due * 75
WHEN nDays_between_return_and_due < 0 THEN -- Returned early
nDays_between_return_and_due * 25
ELSE -- Returned on time
0
END
WHERE CustomerID = :NEW.CustomerID;
END FEE_TRIGGER;
祝你好运。