'How to access value of column from one table in trigger of another table
Created Star Schema where Sales is a fact table and Product_Dimension is one of the dimension tables. Created Product_Dimension table
Create Table Product_Dimension
(Product_ID char(6),
Product_Name varchar(20),
Product_Category varchar(10),
Unit_Price decimal(6,2));
alter table Product_Dimension add constraint pk_Product_Dimension
Primary key(Product_ID);
Created Fact table-sales
Create table Sales(
Product_ID char(5),Order_ID char(5),Customer_ID char(6),Employer_ID
char(5),total int, Quantity int, Discount decimal(5,2),
foreign key (Product_ID) references Product_Dimension(Product_ID),
foreign key (Order_ID) references Time_Dimension(Order_id),
foreign key (Customer_ID) references Customer_Dimension(Customer_ID),
foreign key (Employer_ID) references Emp_Dimension(Emp_ID)
);
I want to create a trigger which calculates value of Total from values of Quantity and Discount from Sales table and Unit_Price from Product_Dimension table.
So every time we insert value in Sales, it should take Quantity(taken from insert query we trying to replace) multiply it with Unit_Price from Product_Dimension table where Product_IDs are matching and subtract Discount(taken from insert query)
Sales_view is a view of Sales table
CREATE or replace TRIGGER Update_Total
instead of insert on Sales_view for each row
declare
y decimal(6,2);
x decimal(6,2);
begin
x:=select pd.Unit_Price from Product_Dimension where
pd.Product_ID=:NEW.Product_ID;
y:=(:NEW.Quantity*x)-:NEW.Discount
insert into
Sales_view(Product_ID,Order_ID,Customer_ID,Employer_ID,total,
Quantity,
Discount)
values(:NEW.Product_ID,:NEW.Order_ID,:NEW.Customer_ID,:NEW.Employer
y,:NEW.Quantity,:NEW.Discount);
end;
This gives following errors in oracle-live sql:
Errors: TRIGGER UPDATE_TOTAL
Line/Col: 6/4 PLS-00103: Encountered the symbol "SELECT" when
expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date <a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set
specification>
<an alternat
Line/Col: 7/1 PLS-00103: Encountered the symbol "Y"
Line/Col: 7/35 PLS-00103: Encountered the symbol ";" when expecting
one
of the following:
. ( ) , * @ % & = - + < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || indicator member submultiset
Line/Col: 12/4 PLS-00103: Encountered the symbol "end-of-file" when
expecting one of the following:
end not pragma final instantiable persistable order
overriding static member constructor map
Solution 1:[1]
Your Trigger syntax is wrong. The correct syntax should look alike -
CREATE OR REPLACE TRIGGER Update_Total
INSTEAD OF INSERT ON Sales_view
FOR EACH ROW
DECLARE
y decimal(6,2);
x decimal(6,2);
BEGIN
SELECT pd.Unit_Price
INTO x
FROM Product_Dimension
WHERE pd.Product_ID=:NEW.Product_ID;
y := (:NEW.Quantity*x) - :NEW.Discount;
INSERT INTO Sales_view (Product_ID, Order_ID, Customer_ID, Employer_ID,
total, Quantity, Discount)
VALUES (:NEW.Product_ID, :NEW.Order_ID, :NEW.Customer_ID, :NEW.Employer,
y,:NEW.Quantity,:NEW.Discount);
END;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ankit Bajpai |
