我對PL/SQL非常陌生,我試圖找出在觸發器上使用百分比的正確方法。這是我的問題:PL/SQL計算觸發器的百分比
我想創建一個觸發器,每當新的價格有減少> 20%觸發器將觸發,只有在一個減少。我以爲我的邏輯是正確的,但我不能得到它的工作:
表:
Product: pid, price, pname
代碼:
set serveroutput on;
show errors;
update product
set price = 3000
where pid = 2;
select * from product;
create or replace trigger percentage
after update on product
for each row when ((((new.price - old.price)/old.price) * 100)>.20)
declare
x product.pname%type;
y varchar(100);
begin
y := (((:new.price - :old.price)/:old.price) * 100);
dbms_output.put_line('The Product: '||:new.pname|| ' price has decreased by: '|| y ||'%');
end;
我繼續得到這樣的錯誤:
6/3 PLS-00103: Encountered the symbol "=" when expecting one of the f ollowing: := . (@ % ;
6/54 PLS-00103: Encountered the symbol ";" when expecting one of the f ollowing: . () , * % & = - + </> at in is mod remainder no t rem <an exponent (**)> <> or != or ~= >= <= <> and or like l ike2 like4 likec between || multiset member submultiset
12/4 PLS-00103: Encountered the symbol "end-of-file" when expecting on e of the following: end not pragma final instantiable order o verriding static member constructor map
在'when'觸發的'子句:new'和':old'需要被無分號指定,「新」和「舊」。其次,'when'條件具有不匹配的括號。 –
已作出更改,但在價格提高時仍然有效。 – user2402107
如果價格上漲,您的表達'(new.price - old.price)/ old.price'將評估爲正數;如果它超過0.2%(請注意你正在進行的轉換),你的觸發器應該會觸發。如果價格下跌,您的表達式將評估爲負數,該數字總是小於「.20」。附:爲什麼當你用'.20'而不是'20'比較時你乘以100? –