我正在进行数据库分配。我正在尝试创建一个触发器,如果客户表中的客户余额超过400,则阻止用户将值插入到章程表中。触发器编译时没有错误但无法正常工作。有什么建议?
CREATE OR REPLACE TRIGGER Unpaid_Balance
BEFORE INSERT ON Charter
FOR EACH ROW
DECLARE
Unpaid DECIMAL;
BEGIN
SELECT Customer_Balance
INTO Unpaid
FROM Customer, Charter
WHERE CUSTOMER.CustomerID = CHARTER.CustomerID;
IF Unpaid > 400 THEN
Raise_application_error(-20003, 'Customer has an unpaid balance of more than
$400');
end if;
end;
答案 0 :(得分:0)
你可能想要类似于此的内容(查看对插入值的引用 - :NEW.CustromerID
- 这就是你在触发器中忘记的内容):
CREATE OR REPLACE TRIGGER Unpaid_Balance
BEFORE INSERT ON Charter
FOR EACH ROW
DECLARE
V_Unpaid DECIMAL;
BEGIN
SELECT Customer_Balance
INTO V_Unpaid
FROM Customer
WHERE CUSTOMER.CustomerID = :NEW.CustomerID
;
--
IF V_Unpaid> 400 THEN
Raise_application_error(-20003, 'Customer has an unpaid balance of more than $400');
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
/
但我强烈建议您不要将业务逻辑放在触发器中。在您的应用程序中执行。使用触发器来控制流量非常困难,因为事情是自动完成的......