简体   繁体   English

如何在另一个表的触发器中访问一个表中列的值

[英]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.

相关问题 Oracle 在插入另一张表期间触发一张表中列/行的更改值 - Oracle trigger change value of column/row from one table during insert of another table 如何触发表来更改另一个表列的值 - How to trigger a table to change the value of another table column MYSQL - 如何在表中的 UPDATE 之后创建触发器,以将另一个表中的列的值增加 +1? - MYSQL - How to create a trigger after an UPDATE in a table, to increment +1 the value of a column from another table? 如何从另一个表的列更新一个表的一列 - how to update one column of one table from column of another table 触发从一个插入的另一个表更新一个表中的字段值 - Trigger to update field value in one table from another that get insertion 根据另一个表的值更新一个表的列值 - Update column value of one table based on values from another table 如何从一个表复制列值并将相同的值作为行存储到另一个表中 - How to copy a column value from one table and store the same value into another table as a row 如何根据另一个表列从一个表中选择一列? - how to select a column from one table according to another table column? 如何在sybase触发器中的特定列更新之前将行从一个表复制到另一个表? - how to copy row from one table to another before update on specific column in sybase trigger? 如何编写将表的某个列的值复制到另一个表的另一列的触发器? - How to write trigger which copies value of a column of a table to another column of another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM