我正在尝试创建一个触发器TrgDisAmount
来读取PROD_NAME
和PRICE
从PRODUCT
表中读取和计算DISCOUNT
表AMOUNT
中SALES
插入的每个新行的SERIAL
and值PCODE
。
这些是表格信息
Table SALES (SERIAL, PCODE, PROD_NAME, PRICE, DISCOUNT, AMOUNT)
Table PRODUCT (PCODE, PROD_NAME, PROD_CAT, PRICE)
Table DISCOUNT (PROD_CAT, DISCOUNT_RATE)
笔记:
- 输入
SERIAL
和PCODE
only 的值,其余列应TrgDisAmount
仅由触发器输入 - 获取
DISCOUNT_RATE
使用函数GetDiscount
。
我已经创建了从 tableGetDiscount
获取的函数。DISCOUNT_RATE
DISCOUNT
这是我的尝试:
create or replace trigger TrgDisAmount
before insert on SALES for each row
begin
if :new.PCODE = :old.PRODUCT.PCODE then
:new.PROD_NAME := :old.PRODUCT.PROD_NAME;
:new.PRICE := :old.PRODUCT.PRICE;
:new.DISCOUNT := :old.product.PRICE / (GetDiscount(:old.PRODUCT.PROD_CAT));
:new.AMOUNT := :new.PRICE - :new.DISCOUNT;
end if;
insert into SALES columns (PROD_NAME, PRICE, DISCOUNT, AMOUNT)
values (:new.PROD_NAME, :new.PRICE, :new.DISCOUNT, :new.AMOUNT);
end;
/
当我运行该块时,它向我显示此错误:
PLS-00049: bad bind variable 'OLD.PRODUCT'
我Table_Name.Column_name
用来到达特定的列。这样做合法吗?
示例输出应如下所示:
SQL> insert into sales values (1,'MB-101',null, null, null, null);
Result in SALES table:
SERIAL : 1,
PCODE : MB-101,
PROD_NAME : IPHONE 6+,
PRICE : 250 ,
DISCOUNT : 25,
AMOUNT : 225