[英]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.创建了星型模式,其中 Sales 是一个事实表,Product_Dimension 是其中一个维度表。 Created Product_Dimension table创建 Product_Dimension 表
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已创建的事实表-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.我想创建一个触发器,它根据 Sales 表中的 Quantity 和 Discount 值以及 Product_Dimension 表中的 Unit_Price 值来计算 Total 的值。
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 中插入值时,它都应该将数量(取自我们试图替换的插入查询)乘以 Product_Dimension 表中的 Unit_Price,其中 Product_ID 匹配并减去折扣(取自插入查询)
Sales_view is a view of Sales table Sales_view是Sales表的视图
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:这会在 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
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.