I'm trying to create a trigger TrgDisAmount
to read PROD_NAME
and PRICE
from PRODUCT
table and to calculate the DISCOUNT
and AMOUNT
of SALES
Table for every new row inserted with the values of SERIAL
and PCODE
.
These are the tables information
Table SALES (SERIAL, PCODE, PROD_NAME, PRICE, DISCOUNT, AMOUNT)
Table PRODUCT (PCODE, PROD_NAME, PROD_CAT, PRICE)
Table DISCOUNT (PROD_CAT, DISCOUNT_RATE)
Note:
- Enter the values for
SERIAL
andPCODE
only, the remaining columns should entered by the triggerTrgDisAmount
only - Get the
DISCOUNT_RATE
using FunctionGetDiscount
.
I have already created GetDiscount
function to get DISCOUNT_RATE
from table DISCOUNT
.
This is my try:
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;
/
When I run that block it shows me this error:
PLS-00049: bad bind variable 'OLD.PRODUCT'
I use Table_Name.Column_name
to reach to the specific column. Is it legal to do it?
Sample output should be like this:
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
When you create a before insert trigger, you only have to set the
:new
values. You don't actually insert into the table. That is the next step in the processing.Also, in an
insert
trigger, only refer to the:new
values, not the:old
. There is no:old
value, which is the specific error you are getting.Then, you need to do a query to get the relevant information from the other tables. The result should look something like this:
The arithmetic for defining the discount looks very suspicious. A 10% discount would be represented as 1.11 using this methodology. However, this is the arithmetic in your question and you provide no sample data to suggest any other method.