簡體   English   中英

如何在另一個表的觸發器中訪問一個表中列的值

[英]How to access value of column from one table in trigger of another table

創建了星型模式,其中 Sales 是一個事實表,Product_Dimension 是其中一個維度表。 創建 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);

已創建的事實表-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)
    );

我想創建一個觸發器,它根據 Sales 表中的 Quantity 和 Discount 值以及 Product_Dimension 表中的 Unit_Price 值來計算 Total 的值。

因此,每次我們在 Sales 中插入值時,它都應該將數量(取自我們試圖替換的插入查詢)乘以 Product_Dimension 表中的 Unit_Price,其中 Product_ID 匹配並減去折扣(取自插入查詢)

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;

這會在 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

您的觸發器語法錯誤。 正確的語法應該看起來像 -

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM