[英]Sql oracle trigger error
嗨,我想知道是否有人可以幫助我調整觸發器。 我僅與觸發器一起使用了僅幾周時間,這對我來說還是很新的。 我的觸發條件是,假設某條船遲交至customer_balance帳戶每天要增加$ 75。 如果該船提早歸還,則每天增加20美元,該船提早歸還到customer_Balance帳戶。 我一直在嘗試使此觸發器正常工作時出錯,因此我想知道是否可以幫助或向我解釋我做錯了什么。 謝謝。
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;
/
好吧,您必須了解,觸發器是一種(在您的情況下)適用於被修改的每一行的工件。 因此,您正在對此進行不必要的工作。
嘗試這個:
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;
/
那應該做。 請記住,在觸發之前,在:NEW上設置的值將最終顯示在表上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.