我在將存盤程序應用于我的資料庫時遇到問題。在我的 order_table 中,我有以下屬性 order_table(orderID、quantity、price、discount)。當數量超過 500 時,我需要將折扣值設定為“是”,并將價格設定為 15% 的折扣。我試圖使用以下語法使用觸發器:
create trigger discount_t
before insert or update on order_table
for each row
begin
if quantity > 500
then (discount = 'Yes') and (price = price - ((price*15)/100);
end if;
end;
使用此語法時,出現以下錯誤:
Errors: TRIGGER DISCOUNT_T
Line/Col: 3/15 PLS-00103: Encountered the symbol "=" when expecting one of the following:
:= . ( @ % ;
Line/Col: 3/59 PLS-00103: Encountered the symbol ";" when expecting one of the following:
) , * & - / at mod remainder rem <an exponent (**)> and or
|| year day
我對 Oracle/Live SQL 不太熟悉,所以有人知道我做錯了什么嗎?
uj5u.com熱心網友回復:
使用:new系結變數來參考行的新值和:=PL/SQL 中的賦值:
create trigger discount_t
before insert or update on order_table
for each row
begin
if :NEW.quantity > 500 then
:NEW.discount := 'Yes';
:NEW.price := 0.85 * :NEW.price;
end if;
end;
/
然后,對于表:
CREATE TABLE order_table (
quantity NUMBER,
discount VARCHAR2(3) DEFAULT 'No',
price NUMBER
);
如果你:
INSERT INTO order_table (quantity, price) VALUES (1000, 100);
SELECT * FROM order_table;
然后輸出是:
數量 折扣 價錢 1000 是的 85
db<>在這里擺弄
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371572.html
上一篇:拆分表的列值并跳過一些單詞
下一篇:對于長度超過32512個字符的字串,PLSQLDeveloper中的dbms_output.put_line失敗并顯示ORA-06502
