[英]Sql oracle trigger error
Hi I was wondering if someone can help me adjust my trigger. 嗨,我想知道是否有人可以帮助我调整触发器。 I have only been working with triggers for only couple of weeks and quite new to it.
我仅与触发器一起使用了仅几周时间,这对我来说还是很新的。 My trigger is suppose to add $75 per day a boat is return late to the customer_balance account.
我的触发条件是,假设某条船迟交至customer_balance帐户每天要增加$ 75。 If the boat is return early then it adds $20 per day the boat was return early to the customer_Balance account.
如果该船提早归还,则每天增加20美元,该船提早归还到customer_Balance帐户。 I keep getting errors trying to get this trigger to work right so I was wondering can help or explain to me what I am doing wrong.
我一直在尝试使此触发器正常工作时出错,因此我想知道是否可以帮助或向我解释我做错了什么。 Thank you.
谢谢。
create or replace trigger trig_penalty
before update of customer_balance
on customer for each row
declare
i number;
s varchar(20);
begin
select customer_balance into s
from customer
where customer_ID =:new.customer_id;
i := :new.charter_returndate - :old.charter_duedate;
if i>=0 then
dbms_output.put_line('Late return fee added');
update customer
set customer_balance=customer_balance+75*i
where customer_id=s;
else
dbms_output.put_line('Early return refund added');
update customer
set customer_balance=customer_balance+20*i
where customer_id=s;
end if;
end;
/
Well you have to understand that a trigger is an artifact that work for (in your case) each row that are being modified. 好吧,您必须了解,触发器是一种(在您的情况下)适用于被修改的每一行的工件。 So you are doing unecessary work on it.
因此,您正在对此进行不必要的工作。
Try this: 尝试这个:
create or replace trigger trig_penalty
before update of customer_balance
on customer for each row
declare
i number;
s varchar(20);
begin
--You dont need to select the id is it is already on :new.customer_id
-- your trigger is for UPDATE
---select customer_balance into s
--- from customer
--- where customer_ID := :new.customer_id;
i := :new.charter_returndate - :old.charter_duedate;
if i>=0 then
--Do not use dbms_output in an internal object.
--dbms_output.put_line('Late return fee added');
--You don't need to run an update for an update trigger you
-- just need to set the new value on :NEW.FIELD
--update customer
-- set customer_balance=customer_balance+75*i
-- where customer_id=s;
--As you select this value I'm using the OLD which means the value that
--Was already on the table
:NEW.customer_balance := :OLD.customer_balance+75*i;
else
--dbms_output.put_line('Early return refund added');
--update customer
-- set customer_balance=customer_balance+20*i
-- where customer_id=s;
:NEW.customer_balance := :OLD.customer_balance+20*i;
end if;
end;
/
That should do it. 那应该做。 Remeber for an BEFORE trigger the value setted on :NEW will end up on the table.
请记住,在触发之前,在:NEW上设置的值将最终显示在表上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.